多线程学习Demo注解(1)
2013-08-29 22:05
225 查看
Thread.Join() 的解释:点击打开链接
using System; using System.Threading; namespace ThreadTest { public class Alpha { public void Beta() { while (true) { Console.WriteLine("当前线程:{0}", Thread.CurrentThread.Name); Console.WriteLine("子线程在作死地跑跑跑!"); } } }; public class Simple { public static void Main() { Thread.CurrentThread.Name = "主线程"; Console.WriteLine("Thread Start/Stop/Join Sample"); Alpha oAlpha = new Alpha(); //这里创建一个线程,使之执行Alpha类的Beta()方法 Thread oThread = new Thread(new ThreadStart(oAlpha.Beta)); oThread.Name = "子线程"; oThread.Start(); //当子线程不在执行状态则一直不跳出循环——>等待子线程进入执行状态 while (!oThread.IsAlive) ; Console.WriteLine("当前线程:{0}, 睡眠一会!", Thread.CurrentThread.Name); //再等待2毫秒, 让子线程有点时间做一下自己的事情 Thread.Sleep(2); Console.WriteLine("当前线程:{0}, 即将要中止子线程!", Thread.CurrentThread.Name); oThread.Abort(); //此处不太明白 oThread.Join(); //子线程阻止主线程, 直到子线程完成自己的工作? 似乎没有太大意义 Console.WriteLine(); Console.WriteLine("Alpha.Beta has finished"); try { Console.WriteLine("Try to restart the Alpha.Beta thread"); //已中止的线程无法再启动! oThread.Start(); } catch (ThreadStateException) { Console.Write("ThreadStateException trying to restart Alpha.Beta. "); Console.WriteLine("Expected since aborted threads cannot be restarted."); Console.ReadLine(); } } } }
相关文章推荐
- 多线程学习Demo注解(2)——lock
- 多线程学习Demo注解(3)——生产者和消费者
- 【Java学习笔记】58:多线程Socket通信的demo
- Spring学习笔记-springMVC基于注解的控制器(Demo)
- Java学习笔记 --- 多线程共享资源Demo
- lombok最全的,最新的学习资料,demo【最全的注解】打开蓝色注解链接
- JAVA学习笔记_demo_多线程_1
- java自定义注解学习(一)_demo小练习
- 多线程学习Demo
- Java基础 注解 学习笔记
- 黑马程序员---多线程的学习1
- Directx11学习笔记【四】 封装一个简单的Dx11DemoBase
- Android学习 - 自定义锁屏demo之滑动解锁
- python多线程学习记录
- 黑马程序员_学习笔记6注解的总结
- struts2学习--注解开发
- 共同学习Java源代码-多线程与并发-FutureTask类(二)
- 【DAY8】关于多线程的学习笔记
- 学习 webpack,并构建一个超简单的 demo
- 多线程学习