简体   繁体   English

创建线程 - Task.Factory.StartNew vs new Thread()

[英]Creating threads - Task.Factory.StartNew vs new Thread()

I am just learning about the new Threading and Parallel libraries in .Net 4 我刚学习.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();

Now I can do: 现在我能做到:

Task t = Task.Factory.StartNew(() =>
{
   ThreadProcedure();
});

What is the difference if any? 有什么区别?

Thanks 谢谢

There is a big difference. 有一个很大的不同。 Tasks are scheduled on the ThreadPool and could even be executed synchronous if appropiate. 任务在ThreadPool上安排,如果合适,甚至可以执行同步。

If you have a long running background work you should specify this by using the correct Task Option. 如果您有长时间运行的后台工作,则应使用正确的任务选项指定此项。

You should prefer Task Parallel Library over explicit thread handling, as it is more optimized. 您应该更喜欢任务并行库而不是显式线程处理,因为它更加优化。 Also you have more features like Continuation. 此外,您还有更多功能,如Continuation。

The task gives you all the goodness of the task API: 该任务为您提供了任务API的所有优点:

  • Adding continuations ( Task.ContinueWith ) 添加延续( Task.ContinueWith
  • Waiting for multiple tasks to complete (either all or any) 等待完成多项任务(全部或任何)
  • Capturing errors in the task and interrogating them later 捕获任务中的错误并在以后查询它们
  • Capturing cancellation (and allowing you to specify cancellation to start with) 捕获取消(并允许您指定取消开始)
  • Potentially having a return value 可能具有回报价值
  • Using await in C# 5 在C#5中使用await
  • Better control over scheduling (if it's going to be long-running, say so when you create the task so the task scheduler can take that into account) 更好地控制调度(如果它将长时间运行,在创建任务时这样说,以便任务调度程序可以考虑到这一点)

Note that in both cases you can make your code slightly simpler with method group conversions: 请注意,在这两种情况下,您都可以使用方法组转换使代码更简单:

DataInThread = new Thread(ThreadProcedure);
// Or...
Task t = Task.Factory.StartNew(ThreadProcedure);

In the first case you are simply starting a new thread while in the second case you are entering in the thread pool. 在第一种情况下,您只是启动一个新线程,而在第二种情况下,您正在进入线程池。

The thread pool job is to share and recycle threads. 线程池作业是共享和回收线程。 It allows to avoid losing a few millisecond every time we need to create a new thread. 它允许每次我们需要创建一个新线程时避免丢失几毫秒。

There are a several ways to enter the thread pool: 有几种方法可以进入线程池:

  • with the TPL (Task Parallel Library) like you did 像你一样使用TPL (任务并行库)
  • by calling ThreadPool.QueueUserWorkItem 通过调用ThreadPool.QueueUserWorkItem
  • by calling BeginInvoke on a delegate 通过在委托上调用BeginInvoke
  • when you use a BackgroundWorker 当您使用BackgroundWorker时

Your first block of code tells CLR to create a Thread (say. T) for you which is can be run as background (use thread pool threads when scheduling T ). 你的第一个代码块告诉CLR为你创建一个Thread(比如.T),它可以作为后台运行(在调度T时使用线程池线程)。 In concise, you explicitly ask CLR to create a thread for you to do something and call Start() method on thread to start. 简而言之,您明确要求CLR为您创建一个线程,并在线程上调用Start()方法来启动。

Your second block of code does the same but delegate (implicitly handover) the responsibility of creating thread (background- which again run in thread pool) and the starting thread through StartNew method in the Task Factory implementation. 您的第二个代码块执行相同操作,但委派(隐式切换)创建线程(后台 - 再次在线程池中运行)和启动线程通过Task Factory实现中的StartNew方法的职责。

This is a quick difference between given code blocks. 这是给定代码块之间的快速差异。 Having said that, there are few detailed difference which you can google or see other answers from my fellow contributors. 话虽如此,您可以谷歌或看到我的其他贡献者的其他答案的细节差异很少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM