What is the difference between task and thread?

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 Task is 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?

Thread is a way of fulfilling that promise. But not every Task needs 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 Task might register a callback to receive the value(s) when they‘re ready.

In particular, the Task does 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 Thread to 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!)

时间: 2024-09-29 21:16:32

What is the difference between task and thread?的相关文章

Difference between Process and thread?

What are the differences between a process and a thread? How are they similar? How can 2 threads communicate? How can 2 process communicate? Both processes and threads are independent sequences of execution. The main difference is that threads (of th

Linux中的task,process, thread 简介

本文的主要目的是介绍在Linux内核中,task,process, thread这3个名字之间的区别和联系.并且和WINDOWS中的相应观念进行比较.如果你已经很清楚了,那么就不用往下看了. LINUX版本:2.6.18ARCH: X86 首先要明确的是,按照LKD 2里面的说法,LINUX和其他OS 比如WINDOWS, SOLARIS之间一个很大的不同是没有严格定义的线程(thread).那么你也许会问,如果LINUX中没有线程,那么如何来表示类似WINDOWS 线程的那种执行观念呢?答案是

C# 的 Task、Thread、ThreadPool 之间有什么异同?

Thread就是Thread,需要自己调度,适合长跑型的操作. ThreadPool是Thread基础上的一个线程池,目的是减少频繁创建线程的开销.线程很贵,要开新的stack,要增加CPU上下文切换,所以ThreadPool适合频繁.短期执行的小操作.调度算法是自适应的,会根据程序执行的模式调整配置,通常不需要自己调度线程.另外分为Worker和IO两个池.IO线程对应Native的overlapped io,Win下利用IO完成端口实现非阻塞IO. Task或者说TPL是一个更上层的封装,N

C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!

说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部分可以同时执行:对于比较耗时的操作(例如io,数据库操作),或者等待响应(如WCF通信)的操作,可以单独开启后台线程来执行,这样主线程就不会阻塞,可以继续往下执行:等到后台线程执行完毕,再通知主线程,然后做出对应操作! 在C#中开启新线程比较简单 static void Main(string[]

[多线程]thread,threadpool,task及TPL知识点整理

简单理解 Thread:是一个指令序列,个体对象. Threadpool:在使用Thread的过程中,程序员要为每个希望并发的序列new一个线程,很麻烦,因此希望有一个统一管理线程的方法,程序员就不需要关注线程的申请管理问题,所以就对Thread进行一系列封装,有了ThreadPool.使用Threadpool,把需要并发的序列添加进线程池,线程池根据其线程列表中的线程的空闲情况,动态为并发序列申请线程. Task:再后来,程序员发现在使用Threadpool的过程当中还是存在很多不便,比如:(

C#中如果正确使用线程Task类和Thread类

C#中使用线程Task类和Thread类小结 刚接触C#3个月左右,原先一直使用C++开发,因为公司的需要,所地采用C#开发,主要是控制设备的实时性操作,此为背景. 对于C#中的Task和Thread我在这不作介绍,要了解更多的,如果查看相当信息.此次项目中使用到TASK和THRED,让我调试足足用了将近两周的时间才找出问题所在,所以在此写出来防止跟我一样刚接触C#,又同时需要对线程的实时性要求的开发人员一些个人总结注意事项. 1.Task适合用于多处理器,且i系列多处理器. 2.Thread则

新手浅谈Task异步编程和Thread多线程编程

初学Task的时候上网搜索,看到很多文章的标题都是task取代thread等等相关,我也一直以为task和thread是一类,其实task是.net4.0提出的异步编程,在之前.net1.0有delegete.beginInoke(XXXX),还有.net2.0中的EAP,在最新的4.5中又有async.await这种新的异步编程.而Thread和Threadpool则是多线程编程. 但是Task也是把任务推到线程池中 1 static void Main(string[] args) 2 {

How to Analyze Java Thread Dumps--reference

原文地址:http://architects.dzone.com/articles/how-analyze-java-thread-dumps The Performance Zone is presented by AppDynamics. AppDynamics is a leaders in the APM space with massive cost reductions for users. The content of this article was originally wri

How to Analyze Java Thread Dumps

When there is an obstacle, or when a Java based Web application is running much slower than expected, we need to use thread dumps. If thread dumps feel like very complicated to you, this article may help you very much. Here I will explain what thread