创建线程 - Task.Factory.StartNew VS新的Thread() [英] Creating threads - Task.Factory.StartNew vs new Thread()
问题描述
我刚学有关.NET中的4
I am just learning about the new Threading and Parallel libraries in .Net 4
在过去,我会创造一个新的主题,像这样(作为一个例子):
In the past I would create a new Thread like so (as an example):
DataInThread = new Thread(new ThreadStart(ThreadProcedure));
DataInThread.IsBackground = true;
DataInThread.Start();
现在我能做的:
Task t = Task.Factory.StartNew(() =>
{
ThreadProcedure();
});
的区别,如果任何?
What is the difference if any?
感谢
推荐答案
有一个很大的区别。任务调度在线程池和甚至可以执行同步如果合适。
There is a big difference. Tasks are scheduled on the ThreadPool and could even be executed synchronous if appropiate.
如果你有一个长期运行的后台工作,你应该使用正确的任务选项指定。
If you have a long running background work you should specify this by using the correct Task Option.
您应该preFER任务并行库了明确的线程处理,因为它是更优化。你也有更多的功能,如延续。
You should prefer Task Parallel Library over explicit thread handling, as it is more optimized. Also you have more features like Continuation.
这篇关于创建线程 - Task.Factory.StartNew VS新的Thread()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!