What is the difference between task and thread?
2016-04-20 10:29
633 查看
http://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread
回答一:
A task is something you want done.
A thread is one of the many possible workers which performs that task.
In .NET 4.0 terms, a Task represents an asynchronous operation. Thread(s) are used to complete that operation by breaking the work up into chunks and assigning to separate threads.
回答2:
In computer science terms, a
A
In particular, the
回答一:
A task is something you want done.
A thread is one of the many possible workers which performs that task.
In .NET 4.0 terms, a Task represents an asynchronous operation. Thread(s) are used to complete that operation by breaking the work up into chunks and assigning to separate threads.
回答2:
In computer science terms, a
Taskis a future or a promise. (Some people use those two terms synomymously, some use them differently, nobody can agree on a precise definition.) Basically, a
Task<T>"promises" to return you a
T, but not right now honey, I'm kinda busy, why don't you come back later?
A
Threadis a way of fulfilling that promise. But not every
Taskneeds a brand-new
Thread. (In fact, creating a thread is often undesirable, because doing so is much more expensive than re-using an existing thread from the threadpool. More on that in a moment.) If the value you are waiting for comes from the filesystem or a database or the network, then there is no need for a thread to sit around and wait for the data when it can be servicing other requests. Instead, the
Taskmight register a callback to receive the value(s) when they're ready.
In particular, the
Taskdoes not say why it is that it takes such a long time to return the value. It mightbe that it takes a long time to compute, or it might that it takes a long time to fetch. Only in the former case would you use a
Threadto run a
Task. (In .NET, threads are freaking expensive, so you generally want to avoid them as much as possible and really only use them if you want to run multiple heavy computations on multiple CPUs. For example, in Windows, a thread weighs 12 KiByte (I think), in Linux, a thread weighs as little as 4 KiByte, in Erlang/BEAM even just 400 Byte. In .NET, it's 1 MiByte!)
相关文章推荐
- web项目自动化
- HTMl5的存储方式sessionStorage和localStorage详解
- HTML5中Access-Control-Allow-Origin解决跨域问题
- cad中关于点样式点的绘制
- testng生成自定义html报告
- JSP九大内置对象
- js数据类型转换
- js 本地图片未上传先预览(等比缩小图片内容大小)
- js常用运算符及用法
- 利用Knockoutjs对电话号码进行验证
- js跨域请求的问题
- angular随笔
- Webpack5分钟入门教程
- JQuery中使用ajax $.ajax()
- JavaScript系列:replace()函数(第二个参数为函数)
- ajax 异步请求后台返回字符串赋值给js参数
- ReactiveCocoa代码实践之-UI组件的RAC信号操作
- js变量数据类型的几个小知识点
- caffe-source code
- 如何选择一个公司即使你拿到了公司offer