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

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

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 中的完整数据的时候,这种方法就可以很好的确保两个线程的同步性。
作者最新文章
做好网站维护需要具备哪些知识? 网站维护都需要做哪些内容?
C# Console 控制台禁止重复打开, 只能运行一个实例, 禁止多开
C# 正则获取网页内容, 抓取html源代码里的 title
C# 通过循环的方式遍历数组中不相同的元素
css 设置背景色的两种方式, 如何设置背景颜色?