c# Thread 的jion方法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Test
{
    class TestThread
    {
        private static void ThreadFuncOne()
        {
            for (int i = 0; i < 150; i++)
            {
                Console.WriteLine("----"+Thread.CurrentThread.Name + " i = " + i);
            }
            Console.WriteLine(Thread.CurrentThread.Name + " has finished");
        }

        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "MainThread";
            Thread newThread = new Thread(new ThreadStart(ThreadFuncOne));
            newThread.Name = "NewThread";
            newThread.Start();

            for (int j = 0; j < 200; j++)
            {
                if (j == 60)
                {
                    newThread.Join();
                }
                else
                {
                    Console.WriteLine(Thread.CurrentThread.Name + " j = " + j);
                }
            }
            Console.Read();
        }
    }
}

线程的jion:

在线程A中调用线程B的jion方法,线程A暂停执行,一直执行完线程B然后才执行线程A。

在A线程中创建线程B,然后启动线程B,当执行线程B的jion方法后,A线程将被阻塞,只执行B线程,一直等到B线程执行完毕,才继续执行A线程。

线程B可以再A中创建,也可以不在A中创建。

 

你可能感兴趣的:(thread)