通过查找一些文章,得知,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.

Task与Thread间的区别的更多相关文章

  1. c#之task与thread区别及其使用

    如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! --------------------------- ...

  2. Linux中的task,process, thread 简介

    本文的主要目的是介绍在Linux内核中,task,process, thread这3个名字之间的区别和联系.并且和WINDOWS中的相应观念进行比较.如果你已经很清楚了,那么就不用往下看了. LINU ...

  3. Task 使用 Task以及Task.Factory都是在.Net 4引用的。Task跟Thread很类似,通过下面例子可以看到。

    static public void ThreadMain() { Thread t1 = new Thread(TaskWorker); t1.Start(3); } static public v ...

  4. array_unique和array_flip 实现去重间的区别

    array_unique和array_flip 实现去重间的区别 ​php有内置函数array_unique可以用来删除数组中的重复值, phperz~com (PHP 4 >= 4.0.1,  ...

  5. Executor, ExecutorService 和 Executors 间的区别与联系

    UML简要类图关系: 下面详细看一下三者的区别: Executor vs ExecutorService vs Executors 正如上面所说,这三者均是 Executor 框架中的一部分.Java ...

  6. java多线程机制中的Thread和Runnable()区别

    1.java语言使用Thread类及其子类对象来表示线程,新建的一个线程声明周期中经历 新建.(声明一个线程,此时他已经有了相应的内存空间和其他资源),运行(线程创建之久就据用了运行的条件,一旦轮到使 ...

  7. innerHTML innerText与outerHTML间的区别

    innerHTML与innerText及outerHTML间的区别最容易使初学者搞混淆,为了更好的使读者区分开.下面我就通过一个demo来解释: 代码: <!DOCTYPE html>&l ...

  8. 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别

    JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...

  9. float与position间的区别

    float与position间的区别:    个人理解为:脱离文档流不一定脱离文本流:但脱离文本流,则也脱离文档流.[如有更好的理解还望评论区一起探讨,共同学习进步]一.float 浮动(脱离文档流, ...

随机推荐

  1. WPFPath素材

    放在Viewbox中可固定比例 <!--五角星--> <Canvas Width="100" Height="100"> <Pat ...

  2. impdp/expdp 总结

      impdp/expdp   1.创建DIRECTORY create directory dir_dp as '/tmp';   --建议将DIRECTORY 建在 /tmp 表下面,该目录肯定存 ...

  3. python3.5文档

    https://docs.python.org/3.5/tutorial/modules.html#packages

  4. 【结构型】Composite模式

    组合模式意在将对象组合成树形结构以表示部分与整体的层次结构关系,并且用户对单个对象的操作以有对组合对象的操作都是一致的.即:组合对象 is-a 单个对象,同时又可以组合着 n 个的单个对象(甚至于其他 ...

  5. IE10的bug?disabled button如何触发事件

    最近碰到一个问题,IE10下的,貌似是个bug,求助! 问题表现为:在内部有dom元素的情况下,disabled状态的button会因为子元素的mouseover产生事件冒泡而触发,也就是说,disa ...

  6. pm2 安装使用

    pm2 是全新开发的进程守护服务, 同时集成了负载均衡功能. 以及开机启动, 自动重启有问题进程. 还可以查看各服务进程状态. 使用方法参照:https://github.com/Unitech/pm ...

  7. uva 10036 Problem C: Divisibility

    题意:能否在一个整数序列的每相邻的两项之间添加一个加减号,使得最终结果能被一个给定整数K<=100整除. dp[i][j]表示第i个数取余k为j的布尔值. #include <cstdio ...

  8. Qt之QTableView显示富文本(使用了QAbstractTextDocumentLayout和QTextDocument)

    http://blog.csdn.net/liang19890820/article/details/50973099

  9. android 截取指定位置字符串

    String str = "s\ziyuan"; String newStr = str.subString(str.indexOf("\\"),str.len ...

  10. MFC界面更新实现方法

    1.更新窗口 即采用UpdateWindow()函数立即发送WM_PAINT消息更新整个窗口. void CEditTestDlg::OnBnClickedBtnSysUpdate() { CStri ...