创建线程 - Task.Factory.StartNew 与 new Thread() [英] Creating threads - Task.Factory.StartNew vs new Thread()

查看:35
本文介绍了创建线程 - Task.Factory.StartNew 与 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();
});

有什么区别?

谢谢

推荐答案

有很大的不同.任务在线程池上调度,如果合适甚至可以同步执行.

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.

与显式线程处理相比,您应该更喜欢任务并行库,因为它更加优化.此外,您还有更多功能,例如 Continuation.

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 与 new Thread()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆