Threading issue: locking data [modified]
-
I'm new to C# threads and I was trying to populate a list with a series of threads but my list is not being populated after the threads finish. Some code might help describe my situation: NOTE: I have left some methods out but I don't believe that will be an issue.
public class ManagerClass { private List<BeanType> beans = new List<BeanType>(); private static Object lockObj = new Object(); private static readonly int MAX_THREADS = 5; public List<BeanType> getBeans() { string[] keys = null; keys = getKeys(); ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS); foreach (string key in keys) { ThreadPool.QueueUserWorkItem(new WaitCallback(getValueThreaded), key); } // Assuming 10000 is enough time for the threads to finish. Thread.Sleep(10000); Console.WriteLine(beans.Capacity); return beans; } public void getValueThreaded(object obj) { string key = (string)obj; string value = getValue(key); lock(lockObj) { beans.Add(new BeanType(key, value)); } } }
That is the jist of it. BeanType is an arbitrary type for the sake of this example. In the console the list prints out a capacity of 0 every time. Can anyone explain this? -- modified at 11:20 Tuesday 6th March, 2007 -- modified at 12:24 Tuesday 6th March, 2007
Kelsen
-
I'm new to C# threads and I was trying to populate a list with a series of threads but my list is not being populated after the threads finish. Some code might help describe my situation: NOTE: I have left some methods out but I don't believe that will be an issue.
public class ManagerClass { private List<BeanType> beans = new List<BeanType>(); private static Object lockObj = new Object(); private static readonly int MAX_THREADS = 5; public List<BeanType> getBeans() { string[] keys = null; keys = getKeys(); ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS); foreach (string key in keys) { ThreadPool.QueueUserWorkItem(new WaitCallback(getValueThreaded), key); } // Assuming 10000 is enough time for the threads to finish. Thread.Sleep(10000); Console.WriteLine(beans.Capacity); return beans; } public void getValueThreaded(object obj) { string key = (string)obj; string value = getValue(key); lock(lockObj) { beans.Add(new BeanType(key, value)); } } }
That is the jist of it. BeanType is an arbitrary type for the sake of this example. In the console the list prints out a capacity of 0 every time. Can anyone explain this? -- modified at 11:20 Tuesday 6th March, 2007 -- modified at 12:24 Tuesday 6th March, 2007
Kelsen
foreach (string key in keys) {
ThreadPool.QueueUserWorkItem(new WaitCallback(getValueThreaded), key);
}
Console.WriteLine(beans.Capacity);Kelsen wrote:
In the console the list prints out a capacity of 0 every time. Can anyone explain this?
Could it be that the console prints out before the thread pool items have a chance to run?
-
foreach (string key in keys) {
ThreadPool.QueueUserWorkItem(new WaitCallback(getValueThreaded), key);
}
Console.WriteLine(beans.Capacity);Kelsen wrote:
In the console the list prints out a capacity of 0 every time. Can anyone explain this?
Could it be that the console prints out before the thread pool items have a chance to run?
Leslie Sanford wrote:
Could it be that the console prints out before the thread pool items have a chance to run?
I migrated the WriteLine outside of the method and it still outputs 0. The code looks like:
public class SomeClass { private static ManagerClass manager = new ManagerClass(); public static void Main() { List beans = manager.getBeans(); Console.WriteLine(beans.Capacity); } }
I also placed a wait at the end of the getBeans method. Any ideas?
Kelsen
-
I'm new to C# threads and I was trying to populate a list with a series of threads but my list is not being populated after the threads finish. Some code might help describe my situation: NOTE: I have left some methods out but I don't believe that will be an issue.
public class ManagerClass { private List<BeanType> beans = new List<BeanType>(); private static Object lockObj = new Object(); private static readonly int MAX_THREADS = 5; public List<BeanType> getBeans() { string[] keys = null; keys = getKeys(); ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS); foreach (string key in keys) { ThreadPool.QueueUserWorkItem(new WaitCallback(getValueThreaded), key); } // Assuming 10000 is enough time for the threads to finish. Thread.Sleep(10000); Console.WriteLine(beans.Capacity); return beans; } public void getValueThreaded(object obj) { string key = (string)obj; string value = getValue(key); lock(lockObj) { beans.Add(new BeanType(key, value)); } } }
That is the jist of it. BeanType is an arbitrary type for the sake of this example. In the console the list prints out a capacity of 0 every time. Can anyone explain this? -- modified at 11:20 Tuesday 6th March, 2007 -- modified at 12:24 Tuesday 6th March, 2007
Kelsen
-
Kelsen wrote:
but my list is not being populated after the threads finish.
Kelsen wrote:
Can anyone explain this?
The threads are NOT "finished" after they are "queued", they are ummmm... queued.
led mike
led mike wrote:
The threads are NOT "finished" after they are "queued", they are ummmm... queued.
After the foreach completes and finishes waiting the threads should have finished executing. An example I wrote prior to posting this question was:
public class ThreadTest { private static readonly int MAX_THREADS = 5; public static void Main() { ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS); for (int i = 0; i < 20; i++) ThreadPool.QueueUserWorkItem(new WaitCallback(processThread), (i + 1)); Thread.Sleep(25000); Console.WriteLine("Finished!"); } static void processThread(object obj) { Console.WriteLine("Thread " + (int)obj + " executing."); Thread.Sleep(5000); } }
This is of course assuming the Thread.Sleep(25000) will be long enough for the last 5 threads to execute. In my original post I didn't show my Thread.Sleep() but it was there within my code. I'll better document that to help clear up any confusion.
Kelsen
-
Kelsen wrote:
but my list is not being populated after the threads finish.
Kelsen wrote:
Can anyone explain this?
The threads are NOT "finished" after they are "queued", they are ummmm... queued.
led mike