通过查找一些文章,得知,Task与Thread不可比。Task是为了利用多CPU多核的机制而将一个大任务不断分解成小任务,这些任务具体由哪一个线程或当前线程执行由OS来决定。如果你想自己控制由哪一个Thread执行,要么自己定议task的scheduling, 要么自己来创建Thread来执行代码。

A "Task" is a piece of work that will execute, and complete at some point in the future.

A "Thread" is how something gets executed.

Typically, when you create a Task, by default (ie: using Task.Factory.StartNew), the Task will get Scheduled to run upon a ThreadPool thread at some point.  However, this is not always true.

The advantage of making this separation is that you are allowing the framework (or yourself, if you use a custom TaskScheduler) to control how your work gets mapped onto available threads.  Typically, you'll have many more work items than threads - you may have one million items to process, but only 8 cores in your system.  In a situation like this, it's much more efficient to use a fixed number of threads, and have each thread process multiple items of work.  By separating "Task" from "Thread", you're breaking this coupling of work==thread.

In general, I would recommend using Task instead of creating your own threads.  This is a much nicer, more powerful, and flexible model to use for development, especially as it allows you to handle exceptions in a very clean manner, allows nice things like continuations to be generated, etc.

So it appears that a Task is the preferred means of coding an asynchronous operation, as much of the work is taken care of by the framework. But on the other hand, Thread is still available for existing code and for cases where you explicitly want to allocate and manage an OS thread.

According to the MSDN reference documentation:

The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces in the .NET Framework version 4. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. In addition, the TPL handles the partitioning of the work, the scheduling of threads on the ThreadPool, cancellation support, state management, and other low-level details. By using TPL, you can maximize the performance of your code while focusing on the work that your program is designed to accomplish.

最新文章

  1. [Tool] Open Live Writer插件开发
  2. tp的极光推送demo
  3. Nodejs的Express完成安装指导
  4. 使用wex5得到的一些教训
  5. 文件上传和下载(可批量上传)——Spring(三)
  6. Trie树:POJ2001
  7. const type& 与 type& 的区别
  8. 【转】visual studio 2012进行C语言开发[图文]
  9. 使用bacula实现Linux的远程备份和还原
  10. unix网络编程第三版源代码ubuntu下配置的问题解决
  11. ffmpeg在Win7 VS2010中debug通过,release出错的问题解决方法
  12. J2EE的13个规范之JDBC
  13. SQL中游标的用法
  14. HBuilder常用快捷键
  15. Java虚拟机系列-Java类加载机制
  16. Alpha阶段敏捷冲刺
  17. python3中的新式类mro查看和C3算法原理
  18. /usr/bin/ld: cannot find -lperconaserverclient_r 解决
  19. springboot-3-aop
  20. Python基础之面向对象(进阶篇)

热门文章

  1. sql server备份相关
  2. a标签增加onclick事件提示未定义function
  3. Debian/Ubuntu手动编译安装MongoDB C++11驱动及驱动测试
  4. codevs 3223 素数密度
  5. HTML资源(推荐)
  6. Linux下使用QQ的几种方式
  7. CSS之Position详解
  8. Qt入门(6)——Qt的界面布局
  9. Contest - 2014 SWJTU ACM 手速测试赛(2014.10.31)
  10. POJ 3723 Conscription