探秘System.Threading系列 第二篇:还是Thread,Thread.Start 和 Thread.Join1.?开始一个Thread开始一个Thre
探秘System.Threading系列 第二篇:还是Thread,Thread.Start 和 Thread.Join
1.?开始一个Thread
开始一个Thread很简单,声明一个Thread实例,然后调用Start方法即可

Thread.Start
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->Thread?threadA?=?new?Thread(new?ThreadStart(WorkMethod));
threadA.Start();
Thread?threadB?=?new?Thread(new?ParameterizedThreadStart(WorkMethodWithParam));
threadB.Start(obj);
2.?Thread.Join阻塞调用线程,直到某个线程终止为止。
我按照msdn上的解释的理解是:
threadA.Join()方法是用来阻塞threadA线程,直到在threadA开始执行之后启动的线程执行完毕(或者Sleep了)之后,才开始执行threadA线程的方法
但是事实并非如我所想,下面是msdn上的例子,先声明了一个thread实例,然后将一个方法放入ThreadPool中执行,并调用thread的Join方法以阻塞thread的执行。显示结果在预料之中

MSDN Thread.Join例子
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using?System;
using?System.Threading;
class?IsThreadPool
{
????static?void?Main()
????{
????????AutoResetEvent?autoEvent?=?new?AutoResetEvent(false);
????????Thread?regularThread?=?
????????????new?Thread(new?ThreadStart(ThreadMethod));
????????regularThread.Start();
????????ThreadPool.QueueUserWorkItem(new?WaitCallback(WorkMethod),?
????????????autoEvent);
????????//?Wait?for?foreground?thread?to?end.
????????regularThread.Join();
????????//?Wait?for?background?thread?to?end.
????????autoEvent.WaitOne();
????}
????static?void?ThreadMethod()
????{
????????Console.WriteLine("ThreadOne,?executing?ThreadMethod,?"?+
????????????"is?{0}from?the?thread?pool.",?
????????????Thread.CurrentThread.IsThreadPoolThread???""?:?"not?");
????}
????static?void?WorkMethod(object?stateInfo)
????{
????????Console.WriteLine("ThreadTwo,?executing?WorkMethod,?"?+
????????????"is?{0}from?the?thread?pool.",?
????????????Thread.CurrentThread.IsThreadPoolThread???""?:?"not?");
????????//?Signal?that?this?thread?is?finished.
????????((AutoResetEvent)stateInfo).Set();
????}
}
执行结果是:ThreadTwo,?executing?WorkMethod,?is?from?the?thread?pool.
ThreadOne,?executing?ThreadMethod,?is?not?from?the?thread?pool.
?
但是当我对这个例子稍作修改之后就出现了不可理解的问题,我在WorkMethod中加上Thread.Sleep让线程池中的线程休眠2秒,这样操作之后执行结果是:


Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->????class?IsThreadPool
????{
????????static?void?Main()
????????{
????????????AutoResetEvent?autoEvent?=?new?AutoResetEvent(false);
????????????Thread?threadA?=
????????????????new?Thread(new?ThreadStart(ThreadMethod));
????????????Thread?threadB?=?new?Thread(new?ParameterizedThreadStart(WorkMethod));
????????????threadA.Start();
????????????//?Wait?for?foreground?thread?to?end.
????????????threadA.Join();
????????????threadB.Start(autoEvent);
????????????//?Wait?for?background?thread?to?end.
????????????autoEvent.WaitOne();
????????????Console.Read();
????????}
????????static?void?ThreadMethod()
????????{
????????????Console.WriteLine("ThreadOne,?executing?ThreadMethod,?"?+
????????????????"is?{0}from?the?thread?pool.",
????????????????Thread.CurrentThread.IsThreadPoolThread???""?:?"not?");
????????}
????????static?void?WorkMethod(object?stateInfo)
????????{
????????????Console.WriteLine("ThreadTwo,?executing?WorkMethod,?"?+
????????????????"is?{0}from?the?thread?pool.",
????????????????Thread.CurrentThread.IsThreadPoolThread???""?:?"not?");
????????????//?Signal?that?this?thread?is?finished.
????????????((AutoResetEvent)stateInfo).Set();
????????}
????}
不知道什么原因,上面的代码中threadA并没有被阻塞,难道是只能阻塞放在ThreadPool中执行的线程?没有道理,我反复做了实验都没有成功,把这个结果贴出来,请大家释疑。