早上起的得早,搂了一眼线程池的内容速记下来。
public static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
Console.WriteLine("Main thread does some work, then sleeps.");
// If you comment out the Sleep, the main thread exits before
// the thread pool task runs. The thread pool uses background
// threads, which do not keep the application running. (This
// is a simple example of a race condition.)
Thread.Sleep(10000);
// If you comment out the Sleep, the main thread exits before
// the thread pool task runs. The thread pool uses background
// threads, which do not keep the application running. (This
// is a simple example of a race condition.)
Thread.Sleep(10000);
Console.WriteLine("Main thread exits.");
Console.ReadLine();
}
}
// This thread procedure performs the task.
static void ThreadProc(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Thread.Sleep(5000);
Console.WriteLine("1111111");
}
static void ThreadProc(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Thread.Sleep(5000);
Console.WriteLine("1111111");
}
static void ThreadProc2(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Thread.Sleep(3000);
Console.WriteLine("222222.");
}
{
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Thread.Sleep(3000);
Console.WriteLine("222222.");
}
Output:
Main thread does some work, then sleeps.
222222.
1111111
Main thread exits.
222222.
1111111
Main thread exits.