相关疑难解决方法(0)

为什么Thread和Task之间的性能差异如此之大?

Windows 7,Intel CORE i3,64位,RAM 4Gb,2.27 GHz
.NET Framework 4.0

我有以下代码:

static void Main(string[] args)
{
    var timer = new Stopwatch();
    timer.Start();

    for (int i = 0; i < 0xFFF; ++i)
    {
        // I use one of the following line at time
        Task.Factory.StartNew(() => { });
        new Thread(() => { }).Start();
    }

    timer.Stop();

    Console.WriteLine(timer.Elapsed.TotalSeconds);
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

如果我使用Task,输出总是小于0.01秒,但如果我使用Thread,输出总是大于40秒!
这怎么可能?为什么这么大的差异?

c# multithreading

27
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

multithreading ×1