c#多线程thread实例详解

2021-06-27 02:06

阅读:611

标签:回调   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 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 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 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 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 list = new List();
            for(int i = 0;i 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 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


评论


亲,登录后才可以留言!