thread生命周期简介
在msdn上http://msdn.microsoft.com/en-us/library/system.threading.threadstate.aspx对于线程的生命周期总结如下:
State | Description |
Running | The thread has been started, it is not blocked, and there is no pending ThreadAbortException . |
StopRequested | The thread is being requested to stop. This is for internal use only. |
SuspendRequested | The thread is being requested to suspend. |
Background | The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting the Thread.IsBackground property. |
Unstarted | The Thread.Start method has not been invoked on the thread. |
Stopped | The thread has stopped. |
WaitSleepJoin | The thread is blocked. This could be the result of calling (We will be covering all of these locking /synchronization techniques in Part 3.) |
Suspended | The thread has been suspended. |
AbortRequested | The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pending System.Threading.ThreadAbortException that will |
下面将详细介绍每个状态。
1.Join方法能够保证两个进程之间的先后运行关系,如下图所示:
示例如下:
程序运行结果:
2.Sleep方法挂起当前的线程,也就是说在给定的时间内当前线程得不到运行的机会(cpu的时间片)
示例:
上面的结果可以看出T1这个线程首先Start,然后是T2,之后T2进程输出了51和52,然后时间片到达,转向T1,T1开始运行,在运行过程中遇到10的话就sleep,这时T1进入WaitSleepJoin状态,此时T1得不到cpu的时间片,于是只能是T2运行,以此类推.....
3.Interrupt
当调用Sleep函数后,线程将进入WaitSleepJoin状态,此时如果想要重新唤醒该线程的话,那么可以使用Interrupt函数,但是需要注意:
"Interrupting a thread arbitrarily is dangerous, however, because any framework or third-party methods in the calling stack could unexpectedly receive the interrupt rather than your intended code. All it would take is for the thread to block briefly on a simple lock or synchronization resource, and any pending interruption would kick in. If the method wasn't designed to be interrupted (with appropriate cleanup code in finally
blocks), objects could be left in an unusable state, or resources incompletely released.
Interrupting a thread is safe when you know exactly where the thread is."
测试代码如下:
4.Abort
Abort函数的使用时比较危险的,个人实际上不主张使用该函数。
"A blocked thread can also be forcibly released via its Abort
method. This has an effect similar to calling Interrupt
, except that a ThreadAbortException
is thrown instead of a ThreadInterruptedException
. Furthermore, the exception will be re-thrown at the end of the catch
block (in an attempt to terminate the thread for good) unless Thread.ResetAbort
is called within the catch
block. In the interim, the thread has a ThreadState
of AbortRequested
.
The big difference, though, between Interrupt
and Abort
is what happens when it's called on a thread that is not blocked. While Interrupt
waits until the thread next blocks before doing anything, Abort
throws an exception on the thread right where it's executing - maybe not even in your code. Aborting a non-blocked thread can have significant consequences."
-- Threading in C#, Joseph Albahari.
下面这个示例将展示如何创建线程的Start, Pause, Resume等操作。
其中需要注意下面几点:
1.如何向一个线程启动时,传递参数?
2.如何暂停一个线程?
3.如何恢复一个线程?
4.示例中如何停止线程?
示例中线程的停止其实是让该worker thread运行完毕实现的。
参考文献和示例代码下载:
http://www.codeproject.com/KB/threads/ThreadingDotNet2.aspx