試圖掌握TPL。任務忽略Thread.Sleep
只是爲了好玩,我試着用隨機睡眠來創建一些任務,看看它是如何處理的。我是針對火災和忘記模式..
static void Main(string[] args)
{
Console.WriteLine("Demonstrating a successful transaction");
Random d = new Random();
for (int i = 0; i < 10; i++)
{
var sleep = d.Next(100, 2000);
Action<int> succes = (int x) =>
{
Thread.Sleep(x);
Console.WriteLine("sleep={2}, Task={0}, Thread={1}: Begin successful transaction",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId, x);
};
Task t1 = Task.Factory.StartNew(() => succes(sleep));
}
Console.ReadLine();
}
但我不明白爲什麼它輸出忽略了睡眠(隨機)
有人可以解釋給我的所有行到控制檯?
只是一個錯字,編輯了原來的代碼。問題仍然是一樣的! – Janus007 2010-10-29 13:16:04