c# 多线程 源码5

C# Thread.Join 方法阻塞调用线程,直到某个线程终止时为止

我们可以这么理解:当NewThread调用Join方法的时候,MainThread就被停止执行,
直到NewThread线程执行完毕

 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading; 
  6.  
  7. namespace ConsoleApplication1 
  8.     class TestThread 
  9.     { 
  10.         private static void ThreadFuncOne() 
  11.         { 
  12.             for (int i = 0; i < 10; i++) 
  13.             { 
  14.                 Console.WriteLine(Thread.CurrentThread.Name + "   i =  " + i); 
  15.             } 
  16.             Console.WriteLine(Thread.CurrentThread.Name + " has finished"); 
  17.         } 
  18.  
  19.         static void Main(string[] args) 
  20.         { 
  21.             Thread.CurrentThread.Name = "MainThread"
  22.  
  23.             Thread newThread = new Thread(new ThreadStart(TestThread.ThreadFuncOne)); 
  24.             newThread.Name = "NewThread"
  25.  
  26.             for (int j = 0; j < 20; j++) 
  27.             { 
  28.                 if (j == 10) 
  29.                 { 
  30.                     newThread.Start(); 
  31.                     newThread.Join(); 
  32.                 } 
  33.                 else 
  34.                 { 
  35.                     Console.WriteLine(Thread.CurrentThread.Name + "   j =  " + j); 
  36.                 } 
  37.             } 
  38.             Console.Read(); 
  39.         } 
  40.     } 
  41. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值