您的位置:首页 > 其它

探秘System.Threading系列 第二篇:还是Thread,Thread.Start 和 Thread.Join

2009-07-14 11:28 435 查看
1. 开始一个Thread
开始一个Thread很简单,声明一个Thread实例,然后调用Start方法即可

Code
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中执行的线程?没有道理,我反复做了实验都没有成功,把这个结果贴出来,请大家释疑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: