Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thread Synchronization program

Thread Synchronization program

Scheduled Pinned Locked Moved C#
tutorialquestionannouncementlounge
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eric_tran
    wrote on last edited by
    #1

    Please show your ability! I'd like to use 5 threads running concurrently. They use the same method AccessData to add 2 random numbers in the ArrayList of 1000 elements. Is there anything wrong in my coding? How to update the ArrayList after each thread? How to terminate if there is only 1 number left in the ArrayList? Thanks so much using System; using System.Collections; using System.Text; using System.Threading; namespace A1_SS { class Program { static object lockObject; static ArrayList space; static void Main() { space = new ArrayList(); for (int i = 1; i <= 1000; i++) { space.Add(i); } lockObject = new object(); Thread t1 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t1.Start(space); Thread t2 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t2.Start(space); Thread t3 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t3.Start(space); Thread t4 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t4.Start(space); Thread t5 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t5.Start(space); } Thread.Sleep(20 * 1000); } public static void AccessData(object param) { ArrayList list = (ArrayList)param; lock (lockObject) { Random rand = new Random(); int x = rand.Next(1, list.Count); list.Remove(x); Console.WriteLine("x = " + x); int y = rand.Next(1, list.Count); list.Remove(y); Console.WriteLine("y = " + y); int newNumber = x + y; Console.WriteLine("newNumber = " + newNumber); list.Add(newNumber); foreach (object obj in list) Console.WriteLine(obj); } } } } eric

    T 1 Reply Last reply
    0
    • E eric_tran

      Please show your ability! I'd like to use 5 threads running concurrently. They use the same method AccessData to add 2 random numbers in the ArrayList of 1000 elements. Is there anything wrong in my coding? How to update the ArrayList after each thread? How to terminate if there is only 1 number left in the ArrayList? Thanks so much using System; using System.Collections; using System.Text; using System.Threading; namespace A1_SS { class Program { static object lockObject; static ArrayList space; static void Main() { space = new ArrayList(); for (int i = 1; i <= 1000; i++) { space.Add(i); } lockObject = new object(); Thread t1 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t1.Start(space); Thread t2 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t2.Start(space); Thread t3 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t3.Start(space); Thread t4 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t4.Start(space); Thread t5 = new Thread(new ParameterizedThreadStart(Program.AccessData)); t5.Start(space); } Thread.Sleep(20 * 1000); } public static void AccessData(object param) { ArrayList list = (ArrayList)param; lock (lockObject) { Random rand = new Random(); int x = rand.Next(1, list.Count); list.Remove(x); Console.WriteLine("x = " + x); int y = rand.Next(1, list.Count); list.Remove(y); Console.WriteLine("y = " + y); int newNumber = x + y; Console.WriteLine("newNumber = " + newNumber); list.Add(newNumber); foreach (object obj in list) Console.WriteLine(obj); } } } } eric

      T Offline
      T Offline
      Tehnoon
      wrote on last edited by
      #2

      Lots of problems in here. 1. Why are you using lock(lockObject), when lockObject is not being modified or even used by any thread at all!!! If you want to allow one thread at a time to modify your Array List, you should use lock(list). 2. In the code: int x = rand.Next(1, list.Count); list.Remove(x); You are assuming that x is always present in the list. Whereas it is a possibility that x might have been removed by any other thread. Instead of removing the actual entry, you should use the indexes by using RemoveAt, and the number should not be x, it should be list[x]. 3. The code written in the function will only execute five times, once per thread. So the list of 1000 will not get exhausted with this. You should use a loop to iterate through the list.

      E 2 Replies Last reply
      0
      • T Tehnoon

        Lots of problems in here. 1. Why are you using lock(lockObject), when lockObject is not being modified or even used by any thread at all!!! If you want to allow one thread at a time to modify your Array List, you should use lock(list). 2. In the code: int x = rand.Next(1, list.Count); list.Remove(x); You are assuming that x is always present in the list. Whereas it is a possibility that x might have been removed by any other thread. Instead of removing the actual entry, you should use the indexes by using RemoveAt, and the number should not be x, it should be list[x]. 3. The code written in the function will only execute five times, once per thread. So the list of 1000 will not get exhausted with this. You should use a loop to iterate through the list.

        E Offline
        E Offline
        eric_tran
        wrote on last edited by
        #3

        Thanks so much But what I mean for x is that I store a random number in ArrayList to x variable, and take that number out of the ArrayList. Please help eric

        1 Reply Last reply
        0
        • T Tehnoon

          Lots of problems in here. 1. Why are you using lock(lockObject), when lockObject is not being modified or even used by any thread at all!!! If you want to allow one thread at a time to modify your Array List, you should use lock(list). 2. In the code: int x = rand.Next(1, list.Count); list.Remove(x); You are assuming that x is always present in the list. Whereas it is a possibility that x might have been removed by any other thread. Instead of removing the actual entry, you should use the indexes by using RemoveAt, and the number should not be x, it should be list[x]. 3. The code written in the function will only execute five times, once per thread. So the list of 1000 will not get exhausted with this. You should use a loop to iterate through the list.

          E Offline
          E Offline
          eric_tran
          wrote on last edited by
          #4

          Ok I got it. I try now Thanks Tehnoon eric

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups