1. Thread线程启动

由于ThreadStart是一个委托,所以可以简化写法

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            for(int i = 0;i< 5;i++)
            {
                ThreadStart method = () => TestThread("无参数");
                Thread thread = new Thread(method);
                thread.Start();
            }

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

        static void TestThread(string name)
        {
            Console.WriteLine("TestThread Start name:{0} 当前线程id:{1} 当前时间:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());

            long sum = 0;

            for (int i = 0; i < 10000000; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End name:{0} 当前线程id:{1} 当前时间:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
        }

image


2. Thread线程传递参数

上面的例子没专递没有参数的,现在开始传递参数

public delegate void ParameterizedThreadStart(object obj);

这个是Thread可以传递参数的方法委托,使用这个委托,可以传递参数。

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            for(int i = 0;i< 5;i++)
            {
                ParameterizedThreadStart method = o => TestThread(o.ToString());
                Thread thread = new Thread(method);
                thread.Start("param" + i);
            }

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

        static void TestThread(string name)
        {
            Console.WriteLine("TestThread Start name:{0} 当前线程id:{1} 当前时间:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());

            long sum = 0;

            for (int i = 0; i < 10000000; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End name:{0} 当前线程id:{1} 当前时间:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
        }

image


3. Thread线程等待

有时候,主线程需要等待子线程执行完后在执行,Thread的线程等待我们使用join()来实现

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            List<Thread> list = new List<Thread>();
            for(int i = 0;i< 5;i++)
            {
                ParameterizedThreadStart method = o => TestThread(o.ToString());
                Thread thread = new Thread(method);
                thread.Start("param" + i);
                list.Add(thread);
            }
            foreach (var t in list)
            {
                t.Join();
            }
            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

        static void TestThread(string name)
        {
            Console.WriteLine("TestThread Start name:{0} 当前线程id:{1} 当前时间:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());

            long sum = 0;

            for (int i = 0; i < 10000000; i++)
            {
                sum += i;
            }
            Console.WriteLine("TestThread End name:{0} 当前线程id:{1} 当前时间:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
        }

image


4. Thread线程获取返回值

Thread没发获取返回值,请注意


5. Thread线程回调

Thread没有回调,我们自定义一个回调

        private void ThreadCallBack(ThreadStart method, Action callback)
        {
            ThreadStart threadStart = () =>
            {
                method.Invoke();
                callback.Invoke();
            };
            Thread thread = new Thread(threadStart);
            thread.Start();
        }