The multi-threaded pass parameters to a thread

 

 

A summary of the following codes:

  When the main program starts, first create an object ThreadSample class, and provides a number of iterations. Then use the object of CountNumbers method to start the thread. The method runs in another thread, but with number 10, the number is the constructor ThreadSample incoming object. So, we just use the same indirect way to pass the number of iterations to another thread.

  Another way is to use the data transmission method Thread.Start. The method receives an object, and the object passed to the thread. To apply this method, the online method of Cheng Zhongqi move must accept a single parameter of type object. It demonstrates the way when creating threadTwo thread. 8 will be passed as an object to the method of the Count, then Count Method is converted to an integer.

  The next way is to use a lambda expression. lambda expression defines a method not belonging to any class. We have created a method, which uses the parameters needed to call another method, and run the method in another thread. When starting threadThree thread, print out the 12 numbers, which is what we pass through the lambda expression, figure.

  Using lambda expression C # object references another approach is called closure. When any local variables in lambda expressions, C # will generate a class, and the variable as a property of the class. So in fact the same as the way threadOne thread used, but we do not define the class, C # compiler will automatically help us to achieve.

  This can cause several problems. For example, if the same variable in the plurality of lambda expressions, they will share the variable values. In the previous example demonstrates this. When starting threadFour and threadFive thread, they will print 20, because before these two threads start variable is changed to 20.

 

     static void Count(object iterations)
        {
            CountNumbers((int)iterations);
        }

        static void CountNumbers(int iterations)
        {
            for (int i = 1; i <= iterations; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
            }
        }

        static void PrintNumber(int number)
        {
            Console.WriteLine(number);
        }

        class ThreadSample
        {
            private readonly int _iterations;

            public ThreadSample(int iterations)
            {
                _iterations = iterations;
            }
            public void CountNumbers()
            {
                for (int i = 1; i <= _iterations; i++)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(0.5));
                    Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
                }
            }
        }

 

 

        static  void the Main ( String [] args) 
        { 
       // first create an object class ThreadSample, and provides a number of iterations
var Sample = new new ThreadSample ( 10 );

       . (. 1) define a first embodiment from the class        / / object CountNumbers method using ThreadSample start the thread
       // this method runs in another thread, but with number 10, the number is the constructor ThreadSample incoming object
var threadOne = new new the thread (sample.CountNumbers); (we except that the same number of iterations is indirectly transmitted to another thread) threadOne.Name = " ThreadOne " ; threadOne.Start (); threadOne.Join (); Console.WriteLine ( " -------------------------- " );       
       (2) second embodiment: Thread.Start using methods such Flyer embodiment only object parameters
       // another way is to use the data transmission method Thread.Start ( for this method, the online method of Cheng Zhongqi move must accept a single parameter of type object )
var threadTwo = new new the Thread (the Count) ; threadTwo.Name = " ThreadTwo " ; threadTwo.Start ( . 8 ); threadTwo.Join (); Console.WriteLine ( " ----------------------- --- " );
       (3) A third way: using lambda expressions. lambda expression defines a method not belonging to any class
       // We created a method, which uses the parameters needed to call another method, and run the method in another thread
varthreadThree =new newthe Thread (() => CountNumbers (12)); threadThree.Name="ThreadThree"; threadThree.Start (); threadThree.Join (); Console.WriteLine ("--------------------------"); int= I10; varthreadFour =new newthe Thread (() =>PrintNumber (I)); I=20 is; var threadFive = new Thread(() => PrintNumber(i)); threadFour.Start(); threadFive.Start(); Console.Read(); }

 

Well, to summarize here.

 

Guess you like

Origin www.cnblogs.com/gougou1981/p/12322764.html