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

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 中的完整数据的时候,这种方法就可以很好的确保两个线程的同步性。

热门推荐