关于Task的线程窃取

示例代码:

         static void Main(string[] args)
{
ThreadPool.SetMaxThreads(, );
object locker = new object();
const int TaskCount = ; Task main = Task.Run(() =>
{
var mainTid = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Main TID:{0}", mainTid);
Thread.Sleep(); Task[] tasks = new Task[TaskCount];
for (int i = ; i < TaskCount; i++)
{
tasks[i] = Task.Run(() =>
{
var tid = Thread.CurrentThread.ManagedThreadId;
lock (locker)
{
if (mainTid == tid)
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(tid);
if (mainTid == tid)
Console.ForegroundColor = ConsoleColor.Gray;
}
Thread.Sleep();
});
}
Task.WaitAll(tasks);
});
main.Wait();
}
上一篇:C语言基础(19)-结构体,联合体,枚举和typedef


下一篇:bzoj 4870: [Shoi2017]组合数问题