c#多线程thread实例详解

Posted yangxi1081

tags:

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

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());
        }

技术分享图片


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());
        }

技术分享图片


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());
        }

技术分享图片


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();
        }




以上是关于c#多线程thread实例详解的主要内容,如果未能解决你的问题,请参考以下文章

多线程详解

多线程 Thread 线程同步 synchronized

C#多线程委托ParameterizedThreadStart应用

C# Thread开启线程的几种方式

C# 多线程 lock 实例

线程的启动(转)