代码1:
代码2:
代码3:
代码4:
代码5:
代码6:
- //在finally里无限执行
- static void Main(string[] args)
- {
- Thread t = new Thread(new ThreadStart(th));
- t.Start();
- Thread.Sleep(1000);
- t.Abort();
- Thread.Sleep(1000);
- }
- static void th()
- {
- int i = 1;
- try
- {
- while(true)
- {
- Console.WriteLine(i++);
- Thread.Sleep(100);
- }
- }
- catch(ThreadAbortException)
- {
- }
- finally
- {
- while(true)
- {
- Console.WriteLine(i++);
- Thread.Sleep(100);
- }
- }
- }
代码2:
- //Thread.ResetAbort() 取消Abort
- static void Main(string[] args)
- {
- Thread t = new Thread(new ThreadStart(th));
- t.Start();
- Thread.Sleep(1000);
- t.Abort(1);
- Thread.Sleep(1000);
- }
- static void th()
- {
- int i = 1;
- while(true)
- try
- {
- Console.WriteLine(i++);
- Thread.Sleep(100);
- }
- catch(ThreadAbortException)
- {
- Thread.ResetAbort();
- }
- }
代码3:
- //等待自己结束,而自己又不可能结束。
- static void Main(string[] args)
- {
- Thread t = Thread.CurrentThread;
- Console.WriteLine("Join...");
- t.Join();
- }
代码4:
- //死锁
- class Program
- {
- static void Main(string[] args)
- {
- Program p = new Program();
- new Thread(new ThreadStart(p.th1)).Start();
- new Thread(new ThreadStart(p.th2)).Start();
- }
- void th1()
- {
- lock(this)
- Monitor.Wait(this);
- }
- void th2()
- {
- lock(this)
- Monitor.Wait(this);
- }
- }
代码5:
代码6: