Thread Synchronization program
-
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 -
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); } } } }
ericLots 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.
-
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.
-
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.