Task与Thread间的区别
通过查找一些文章,得知,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间的区别的更多相关文章
- c#之task与thread区别及其使用
如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! --------------------------- ...
- Linux中的task,process, thread 简介
本文的主要目的是介绍在Linux内核中,task,process, thread这3个名字之间的区别和联系.并且和WINDOWS中的相应观念进行比较.如果你已经很清楚了,那么就不用往下看了. LINU ...
- Task 使用 Task以及Task.Factory都是在.Net 4引用的。Task跟Thread很类似,通过下面例子可以看到。
static public void ThreadMain() { Thread t1 = new Thread(TaskWorker); t1.Start(3); } static public v ...
- array_unique和array_flip 实现去重间的区别
array_unique和array_flip 实现去重间的区别 php有内置函数array_unique可以用来删除数组中的重复值, phperz~com (PHP 4 >= 4.0.1, ...
- Executor, ExecutorService 和 Executors 间的区别与联系
UML简要类图关系: 下面详细看一下三者的区别: Executor vs ExecutorService vs Executors 正如上面所说,这三者均是 Executor 框架中的一部分.Java ...
- java多线程机制中的Thread和Runnable()区别
1.java语言使用Thread类及其子类对象来表示线程,新建的一个线程声明周期中经历 新建.(声明一个线程,此时他已经有了相应的内存空间和其他资源),运行(线程创建之久就据用了运行的条件,一旦轮到使 ...
- innerHTML innerText与outerHTML间的区别
innerHTML与innerText及outerHTML间的区别最容易使初学者搞混淆,为了更好的使读者区分开.下面我就通过一个demo来解释: 代码: <!DOCTYPE html>&l ...
- 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别
JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...
- float与position间的区别
float与position间的区别: 个人理解为:脱离文档流不一定脱离文本流:但脱离文本流,则也脱离文档流.[如有更好的理解还望评论区一起探讨,共同学习进步]一.float 浮动(脱离文档流, ...
随机推荐
- JAVA-2-DATA
import java.util.*; public class Ch0310 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...
- [转]extern,static存储空间矛盾
其实,这两个语句的位置不同,会出现不同的解释.这主要是由于 static 具有的两重意义所导致的: (1) 如果 static int foo; 这一句位于函数中,则 static 表示的是存储属性, ...
- 《Linux内核分析》 week5作业-system call中断处理过程
一.使用gdb跟踪分析一个系统调用内核函数 1.在test.c文件中添加time函数与采用c语言内嵌汇编的time函数.具体实现请看下图. 2.然后在main函数中添加MenuConfig函数,进行注 ...
- AIDL Service
开发AIDL服务的步骤 AIDL(Android Interface Definition Language)是Service的一种重要应用,允许一个应用程序访问另一个应用程序中的对象. 建立AIDL ...
- 在ubuntu14.14 安装php扩展扩展出现的问题
我是在ubuntu14.14 安装的 lnmp. 部分扩展.均已安装好,但是我用apt-get 方式安装 redis和curl扩展时,我的配置都设置但是从phpinfo里面看没有响应的配置项. 于是我 ...
- Jquery插件模版
;(function($){ $.fn.jcDate = function(options) { var defaults = { IcoClass : "jcDateIco", ...
- 文成小盆友python-num11-(2) python操作Memcache Redis
本部分主要内容: python操作memcache python操作redis 一.python 操作 memcache memcache是一套分布式的高速缓存系统,由LiveJournal的Brad ...
- fabric自动化部署django
使用fabric部署django应用 使用fabric部署django应用 本文是我的网站易读中文网自动化部署的脚本实现,以下代码在ubuntu和debian中测试通过 由于网站使用的是python技 ...
- d023: 各位数字之和
内容: 求输入的一个整数的各位数字之和 输入说明: 一行一个整数 输出说明: 一个整数 输入样例: 2147483646 输出样例 : 45 #include <stdio.h> int ...
- ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片
解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...