【发布时间】:2014-12-27 18:16:16
【问题描述】:
考虑下面的代码。
在主要
for (int i = 0; i <= 9; i++)
{
ThreadPool.QueueUserWorkItem(Hi,i);
}
static void Hi(object arg)
{
int n = (int)arg;
Console.WriteLine("Thread ID:{0} IsBackground:{1} HI Joshua.. You got:{2} ", Thread.CurrentThread.ManagedThreadId,
Thread.CurrentThread.IsBackground,n);
}
我得到以下输出
Thread ID:10 IsBackground:True HI Joshua.. You got:2
Thread ID:10 IsBackground:True HI Joshua.. You got:3
Thread ID:10 IsBackground:True HI Joshua.. You got:4
Thread ID:10 IsBackground:True HI Joshua.. You got:6
Thread ID:10 IsBackground:True HI Joshua.. You got:7
Thread ID:10 IsBackground:True HI Joshua.. You got:8
Thread ID:10 IsBackground:True HI Joshua.. You got:9
Thread ID:11 IsBackground:True HI Joshua.. You got:5
Thread ID:13 IsBackground:True HI Joshua.. You got:1
Thread ID:12 IsBackground:True HI Joshua.. You got:0
我的问题是,既然线程池线程具有背景属性 true 那么为什么它们会出现在我的输出中? 如果有人可以指导我参考或书籍,我可以在其中获得线程池工作的示例,那也很好。
【问题讨论】:
标签: c# .net multithreading visual-studio-2013 threadpool