多线程05-传参

Posted shidengyun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程05-传参相关的知识,希望对你有一定的参考价值。

    class Program
    {
        static void Main()
        {
            var sample = new ThreadSample(10);
            var threadOne = new Thread(sample.CountNumbers);
            threadOne.Name = "ThreadOne";
            threadOne.Start();
            threadOne.Join();
            Console.WriteLine("end threadOne");

            var threadSecond = new Thread(Count);
            threadSecond.Name = "threadSecond";
            threadSecond.Start(10);
            threadSecond.Join();
            Console.WriteLine("end threadSecond");

            var threadThree = new Thread(() => CountNumbers(10));
            threadThree.Name = "threadThree";
            threadThree.Start();
            threadThree.Join();
            Console.WriteLine("end  threadThree");
        }
        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("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
            }
        }
        static void PrintNumbers(int number)
        {
            Console.WriteLine(number);
        }
        class ThreadSample
        {
            private readonly int _interations;
            public ThreadSample(int interations)
            {
                _interations = interations;
            }
            public void CountNumbers()
            {
                for (int i = 1; i < _interations; i++)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(0.5));
                    Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
                }
            }
        }
    }

以上是关于多线程05-传参的主要内容,如果未能解决你的问题,请参考以下文章

多线程启动线程:需要传参和不需要传参两种情况

C++11多线程 多线程传参详解

c++11 thread多线程创建和传参

java 多线程死循环怎么动态传参?急!

多线程动态传参问题

python多线程thread.start_new_thread传参的问题