C#线程(三、线程同步技术之Join方法)

来源:互联网 发布:非农数据实时网址 编辑:程序博客网 时间:2024/06/05 06:15

转自:http://blog.csdn.net/lyh916/article/details/46359027;http://www.cnblogs.com/lxblog/archive/2012/12/26/2833974.html。


sleep:静态方法,只能暂停当前线程,不能暂停其他线程。
join:实例方法,阻塞调用线程,直到该线程终止或经过了指定时间为止。


示例:

using System;  using System.Threading;    class ThreadTest14  {      static void Main(string[] args)      {          Calculate calculate = new Calculate();          Console.ForegroundColor = ConsoleColor.White;          Console.WriteLine("主线程输出:准备进行加法运算:");          calculate.threadAdd.Start();            calculate.threadAdd.Join();          Console.ForegroundColor = ConsoleColor.White;          Console.WriteLine("主线程输出:运算完毕");          Console.ReadKey();      }  }    //计算类  public class Calculate  {      public Thread threadAdd;        public Calculate()      {          threadAdd = new Thread(new ThreadStart(Add));      }        //加法运算      public void Add()      {          Console.ForegroundColor = ConsoleColor.Blue;          Console.WriteLine("进入加法计算");          Thread.Sleep(5000);          Console.ForegroundColor = ConsoleColor.Blue;          Console.WriteLine("加法运算结果: x={0} y={1} x+y={2}", 1, 2, 1 + 2);      }  }  

分析:
调用线程是主线程,当主线程调用了calculate.threadAdd.Join()的时候,就发生了阻塞,直到加法线程运行完毕之后,才继续运行。


现在我们在来看看Join的另外两个重载方法:Join(Int32) 和 Join(TimeSpan),这两个方法其实是一样的,输入参数说白了就是设置阻塞的等待时间,返回值是bool类型,如果线程已终止,则为 true,否则返回 false 。
using System;  using System.Threading;    class ThreadTest15  {      static void Main(string[] args)      {          Calculate calculate = new Calculate();          Console.ForegroundColor = ConsoleColor.White;          Console.WriteLine("主线程输出:准备进行加法和减法两种运算:");            calculate.threadAdd.Start();          calculate.threadSub.Start();          calculate.threadAdd.Join();          calculate.threadSub.Join();            Console.ForegroundColor = ConsoleColor.White;          Console.WriteLine("主线程输出:所有运算完毕");          Console.ReadKey();      }  }    //计算类  public class Calculate  {      public Thread threadAdd;      public Thread threadSub;      public Calculate()      {          threadAdd = new Thread(new ThreadStart(Add));          threadSub = new Thread(new ThreadStart(Sub));      }        //加法运算      public void Add()      {          Console.ForegroundColor = ConsoleColor.Blue;          Console.WriteLine("进入加法计算");          Thread.Sleep(5000);          Console.ForegroundColor = ConsoleColor.Blue;          Console.WriteLine("加法运算结果: x={0} y={1} x+y={2}", 1, 2, 1 + 2);      }        //新增减法运算       public void Sub()      {          //主要是这里          bool b = threadAdd.Join(1000);          Console.ForegroundColor = ConsoleColor.Red;          if (b)          {              Console.WriteLine("加法运算已经完成,进入减法法计算");          }          else          {              Console.WriteLine("加法运算超时,先进入减法法计算");          }            Thread.Sleep(2000);          Console.WriteLine("进入减法运算");          Console.ForegroundColor = ConsoleColor.Red;          Console.WriteLine("减法运算结果: x={0} y={1} x-y={2}", 10, 2, 10 - 2);      }  }  

分析:
首先,主线程遇到 calculate.threadAdd.Join();  和 calculate.threadSub.Join(); 肯定会发生阻塞,等待这两个线程完成后,才会继续执行,这个不容质疑。

然后我们看加法线程和减法线程,这两个线程几乎同时执行,谁先执行,我们是不可预期的。比如先执行加法线程,当执行到Thread.Sleep(5000)的时候,加法线程休眠5s,减法线程由于调用了 threadAdd.Join(1000); 所以减法线程会阻塞1s(此时调用线程是减法线程) ,1s 之后由于加法线程还没有执行完成,所以返回值为 false,减法线程继续执行,减法线程执行完毕后,又过了一会,加法线程才继续执行。这样就会得出我们上面的运行结果。