1. 开始一个Thread
开始一个Thread很简单,声明一个Thread实例,然后调用Start方法即可
Thread.Start
Thread threadA = new Thread(new ThreadStart(WorkMethod));
threadA.Start();
Thread threadB = new Thread(new ParameterizedThreadStart(WorkMethodWithParam));
threadB.Start(obj);
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例子
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();
}
}
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.
ThreadTwo, executing WorkMethod, is from the thread pool.
ThreadOne, executing ThreadMethod, is not from the thread pool.
但是当我对这个例子稍作修改之后就出现了不可理解的问题,我在WorkMethod中加上Thread.Sleep让线程池中的线程休眠2秒,这样操作之后执行结果是:
执行结果
ThreadTwo, executing WorkMethod, is from the thread pool.
Sleep
ThreadOne, executing ThreadMethod, is not from the thread pool.
Sleep end
ThreadTwo, executing WorkMethod, is from the thread pool.
Sleep
ThreadOne, executing ThreadMethod, is not from the thread pool.
Sleep end
可以看出当线程池中的线程Sleep之后,被Join的thread即获得执行权,开始执行,这个似乎和msdn的解释有出入
另外我还做了一个测试,把另一个线程用Thread启动,而不是放在线程池中执行,看是否可以正常的阻塞线程,但是结果却是出人意料的,不能按预期阻塞,请看下面的实例代码:
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();
}
}
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中执行的线程?没有道理,我反复做了实验都没有成功,把这个结果贴出来,请大家释疑。