C# Thread.Join() 的用法, 线程按顺序执行

发布时间:2020-12-08 11:39 作者:独孤剑 阅读:689

Thread.Join() 在MSDN中的解释:Blocks the calling thread until the thread represented by this instance terminates.(在此实例表示的线程终止前,阻止调用线程。)

实例应用
Test2 在 thread1.Join() 被调用后被阻塞,直到 Test1 执行完毕才继续执行。
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(new ThreadStart(Test1));
            thread1.Start();
            thread1.Join();

            Thread thread2 = new Thread(new ThreadStart(Test2));
            thread2.Start();
            thread2.Join();

            Console.WriteLine("finished.");
            Console.Read();
        }

        static void Test1()
        {
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t1 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
        }

        static void Test2()
        {
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
            Console.WriteLine("t2 " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            Thread.Sleep(500);
        }
thread1 启动后,调用 Join() 方法,直到 thread1 的任务结束,才轮到 thread2 启动,然后 thread2 也开始任务。实例中,两个线程就按着严格的顺序来执行了。如果 thread2 的执行需要依赖于 thread1 中的完整数据的时候,这种方法就可以很好的确保两个线程的同步性。

微信打赏, 微信扫一扫

支付宝打赏, 支付宝扫一扫

如果文章对您有帮助,欢迎给作者打赏

作者最新文章
拜登称政府正调查燃油公司黑客攻击事件 确定该公司网络被勒索软件感染
云南大理森林消防直升机坠入洱海:4名机组人员不幸遇难
jquery $.ajax()方法参数详解
java 使用 itext 导出 PDF 文本绝对定位, itext 绝对定位的方法
杭州野生动物世界“金钱豹出逃”发布致歉声明
企业信息查询系统