首页 > 学院 > 开发设计 > 正文

C#中Thread类中Join方法的理解(转载)

2019-11-17 03:04:09
字体:
来源:转载
供稿:网友
C#中Thread类中Join方法的理解(转载)指在一线程里面调用另一线程join方法时,表示将本线程阻塞直至另一线程终止时再执行 比如java代码收藏代码
  1. usingSystem;
  2. namespaceTestThreadJoin
  3. {
  4. classPRogram
  5. {
  6. staticvoidMain()
  7. {
  8. System.Threading.Threadx=newSystem.Threading.Thread(newSystem.Threading.ThreadStart(f1));
  9. x.Start();
  10. Console.WriteLine("ThisisMain.{0}",1);
  11. x.Join();
  12. Console.WriteLine("ThisisMain.{0}",2);
  13. Console.ReadLine();
  14. }
  15. staticvoidf1()
  16. {
  17. System.Threading.Thready=newSystem.Threading.Thread(newSystem.Threading.ThreadStart(f2));
  18. y.Start();
  19. y.Join();
  20. Console.WriteLine("ThisisF1.{0}",1);
  21. }
  22. staticvoidf2()
  23. {
  24. Console.WriteLine("ThisisF2.{0}",1);
  25. }
  26. }
  27. }
这儿有三个线程在处理(包括主线程),大家可看看执行结果. 结果: This is Main.1 This is F2.1 This is F1.1 This is Main.2如果: 注释// x.Join(); 结果: This is Main.1 This is Main.2 This is F2.1 This is F1.1
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表