码迷,mamicode.com
首页 > 编程语言 > 详细

c#多线程thread实例详解

时间:2018-09-16 15:23:53      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:回调   log   join()   str   https   man   call   date   param   

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实例详解

标签:回调   log   join()   str   https   man   call   date   param   

原文地址:https://www.cnblogs.com/yangxi1081/p/9655873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!