探秘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中执行的线程?没有道理,我反复做了实验都没有成功,把这个结果贴出来,请大家释疑。
开始一个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中执行的线程?没有道理,我反复做了实验都没有成功,把这个结果贴出来,请大家释疑。
相关文章推荐
- Java多线程系列--【基础篇03】- Thread中start()和run()的区别
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Thread系列1-浅析 Java Thread.join()
- Java多线程系列(2)--Thread中start()和run()
- Java之Thread线程相关yield()、sleep()、wait()、join()、run和start区别详解
- Java多线程系列--“基础篇”之 Thread中start()和run()的区别
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Thread系列之Thread.Join()
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Thread.join()在start()的前后顺序分析
- ASP.NET温故而知新学习系列之ASP.NET多线程编程—.NET下的多线程编程利用thread.Start()传递参数(四)
- java 多线程系列基础篇(八)之join()、start()、run()方法
- Binder学习之startThreadPool,joinThreadPool
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- WPF QuickStart系列之线程模型(Thread Model)
- java之Thread线程相关yield()、sleep()、wait()、join()、run和start区别详解
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别 (r)