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 浮动(脱离文档流, ...
随机推荐
- JavaWeb学习笔记之Servlet(二)
1. GenericServlet 抽象类: 1). 是一个 Serlvet. 是 Servlet 接口和 ServletConfig 接口的实现类. 但是一个抽象类. 其中的 service 方法 ...
- Revenge of Fibonacc
算法:搜索: In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence rela ...
- Debian/Ubuntu手动编译安装MongoDB C++11驱动及驱动测试
本文章仅限cnblogs网站内转载!请某网站自觉,遵纪守法,尊重原创! 系统环境情况: 最小化.无桌面环境 新安装的Debian 8 Server 版本操作系统虚拟机一台 手动编译安装MongoDB ...
- Cocos2d-x 使用物理引擎进行碰撞检测
[转自]: http://blog.csdn.net/cbbbc/article/details/38541099 通常在游戏简单逻辑判断和模拟真实的物理世界时,我们只需要在定时器中判断游戏中各个精灵 ...
- Chrome下的语音控制框架MyVoix.js使用篇(四)
在上一篇博文中,我为大家介绍了myvoix.js中的smart learning模块,以及何如使用该功能.(myvoix.js的源码地址会在每一篇文章末尾放出) 文本将拓展 Chrome下的语音控制框 ...
- Effective Go -> Interface
1.接口实现及类型转换 type Sequence []int // Methods required by sort.Interface. func (s Sequence) Len() int { ...
- Android中的webview的进度条
<application android:icon="@drawable/hunqin" android:label="@string/app_name" ...
- 51单片机 Keil C 延时程序的简单研究
应用单片机的时候,经常会遇到需要短时间延时的情况.需要的延时时间很短,一般都是几十到几百微妙(us).有时候还需要很高的精度,比如用单片机驱动DS18B20的时候,误差容许的范围在十几us以内,不然很 ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
- Alias Method解决随机类型概率问题
举个例子,游戏中玩家推倒了一个boss,会按如下概率掉落物品:10%掉武器 20%掉饰品 30%掉戒指 40%掉披风.现在要给出下一个掉落的物品类型,或者说一个掉落的随机序列,要求符合上述概率. 一般 ...