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. Threading issue: locking data [modified]

Threading issue: locking data [modified]

Scheduled Pinned Locked Moved C#
helpcsharptutorialquestion
6 Posts 3 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.
  • K Offline
    K Offline
    Kelsen
    wrote on last edited by
    #1

    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

    L L 2 Replies Last reply
    0
    • K 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

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      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?

      K 1 Reply Last reply
      0
      • L Leslie Sanford

        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?

        K Offline
        K Offline
        Kelsen
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K 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

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          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

          K 2 Replies Last reply
          0
          • L led mike

            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

            K Offline
            K Offline
            Kelsen
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • L led mike

              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

              K Offline
              K Offline
              Kelsen
              wrote on last edited by
              #6

              led mike wrote:

              The threads are NOT "finished" after they are "queued", they are ummmm... queued.

              Yup. Figured it out.

              Kelsen

              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