.net 多线程初步(1)

Thread类常用的几个基本方法


1.Start、Join和Sleep
这是线程操作最基本的了,启动、销毁和延迟。


using System;
using System.Threading;

public class StartJoinSleep
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(firstThread);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(1000);
  Console.WriteLine("Now execute join method.");
  myThread.Abort();
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();
 }
 private static void firstThread()
 {
  for ( int i = 1 ; i <= 100 ; i ++)
  {
   Console.WriteLine("First Thread Running. and loop times is {0}.",i);
   Thread.Sleep(200);
  }
 }
}


2.Abort和ResetAbort

Abort在调用此方法的线程上引发 ThreadAbortException,以开始终止此线程的过程。调用此方法通常会终止线程。通常搭配ResetAbort来阻止ThreadAbortException结束线程,用以继续执行finally内的代码
示例:
using System;
using System.Threading;

public class AbortResetAbort
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(Work);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(300);
  Console.WriteLine("Now execute Abort Method.");
  myThread.Abort();
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();   
 }

 public static void Work()
 {
  try
  {
   for(int i=0; i<100; i++)
   {
    Console.WriteLine("Thread - working.");
    Thread.Sleep(100);
   }
  }
  catch(ThreadAbortException e)
  {
   Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
   Console.WriteLine("Exception message: {0}", e.Message);
   Thread.ResetAbort();
  }
  Console.WriteLine("Thread - still alive and working.");
  Thread.Sleep(1000);
  Console.WriteLine("Thread - finished working.");
 }
}

3.Suspend和Resume
线程的挂起与恢复
using System;
using System.Threading;

public class SuspendResume
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(Work);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(500);
  Console.WriteLine("Now execute Suspend Method.2000ms");
  myThread.Suspend();
  Thread.Sleep(2000);
  myThread.Resume();
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();   
 }

 public static void Work()
 {
  for(int i=1; i < 21; i++)
  {
   Console.WriteLine("Thread Loop Times is {0}.",i);
   Thread.Sleep(100);
  }
 }
}

4.Interrupt
将线程在下次以wait、sleep或join状态阻塞的时候中断,下面示例可以查看在Interrupt执行前延迟500ms和550ms时线程内循环次数的区别,可以很清晰的看到触发时间。
using System;
using System.Threading;

public class Interrupt
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(Work);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(500);
  Thread.Sleep(50);
  Console.WriteLine("Now execute Interrupt Method.");
  myThread.Interrupt();
  Thread.Sleep(2000);
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();   
 }
 
 public static void Work()
 {
  int i = 1;
  try
  {
   for(; i < 21; i++)
   {
    Console.WriteLine("Thread Loop Times is {0}.",i);
    Thread.Sleep(100);
   }
  }
  catch(ThreadInterruptedException ex)
  {
   Console.WriteLine("I value is {0}",i);
   Console.WriteLine("Interrupted:{0}",ex.Message);
  }
  finally
  {
   Console.WriteLine("In Finally State");
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值