Mon*_*mer 6 c# multithreading task
考虑以下代码。
static void Main(string[] args)
{
Thread t = new Thread(Foo);
t.Start();
Console.WriteLine("Main ends.");
//t.Join();
}
static void Foo()
{
for (int x = 0; x < 1000000000; x++) ;
Console.WriteLine("Foo ends.");
}
Run Code Online (Sandbox Code Playgroud)
static void Main(string[] args)
{
Task t = new Task (Foo);
t.Start();
Console.WriteLine("Main ends.");
t.Wait();
}
static void Foo()
{
for (int x = 0; x < 1000000000; x++) ;
Console.WriteLine("Foo ends.");
}
Run Code Online (Sandbox Code Playgroud)
使用时Task,我们需要t.Wait()在主线程结束之前等待线程池线程完成,但使用时Thread,我们不需要t.Join得到相同的效果。
为什么t.Join()不需要阻止主线程在其他生成的线程结束之前结束?
| 归档时间: |
|
| 查看次数: |
7824 次 |
| 最近记录: |