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");
}
}
}