Thread in depth 2:Asynchronization and Task
When we want to do a work asynchronously, creating a new thread is a good way. .NET provides two others ways rather than create thread explicitly, that is ThreadPool and Task.
ThreadPool
Every single CLR maintains a threadpool.There is a queue for user action request inside the threadpool.Everytime an action entry is queued by calling the ThreadPool.QueueUserWorkItem method, threadpool will pick up or create a thread to do the work.When the work is done,the thread will be returned to threadpool rather than be destroyed.
Task
ThreadPool is deficient that you can hardly know when is the work done and hardly get the returning result. In this case, Task may be a better answear.
- You can do a work asynchronously by calling Task.Run() method or the start() method of a instance of Task.
 - Invoke the Wait() method to block the caller thread and wait until the task is done, meanwhile, access to the Result property of a task instance will block the caller thread, because it will call wait() method inside the Result property.
 - Task supports cancellation just as ThreadPool do, by using the CancellationTokenSource.
 - Can arrange one or more continuous works to be executed as soon as the task is done, by invoking the ContinueWith() method.But note that the continuous works may not be executed in the same thread of the origin work.
 
async and await
When access to the Result property to get the returning result of a task, the caller thread will be blocked to wait for the task get done. In this case, you can use the "await" keyword to let the control return to the caller thread while waitting for the result of the task.
If await is used inside a method, then the async modifier is required on the method, and, if a method is decorated by async, the return type of this method should only be void or Task or Task<TResult> .
Thread in depth 2:Asynchronization and Task的更多相关文章
- Thread in depth 4:Synchronous primitives
		
There are some synchronization primitives in .NET used to achieve thread synchronization Monitor c# ...
 - Thread in depth 3:Synchronization
		
Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...
 - Thread in depth 1: The basic
		
Every single thread has the follow elements: Execution Context:Every thread has a execution context ...
 - Task  使用  Task以及Task.Factory都是在.Net 4引用的。Task跟Thread很类似,通过下面例子可以看到。
		
static public void ThreadMain() { Thread t1 = new Thread(TaskWorker); t1.Start(3); } static public v ...
 - Task与Thread间的区别
		
通过查找一些文章,得知,Task与Thread不可比.Task是为了利用多CPU多核的机制而将一个大任务不断分解成小任务,这些任务具体由哪一个线程或当前线程执行由OS来决定.如果你想自己控制由哪一个T ...
 - C#中假设正确使用线程Task类和Thread类
		
C#中使用线程Task类和Thread类小结 刚接触C#3个月左右.原先一直使用C++开发.由于公司的须要,所地採用C#开发.主要是控制设备的实时性操作,此为背景. 对于C#中的Task和Thread ...
 - C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!
		
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
 - Thread.Join 和 Task.Wait 方法
		
这两个方法 可以说是类似的功能,都是对当前任务进行等待阻塞,执行完毕后再进行后续处理 talk is cheap, show you code,下面一个是异步执行,一个是加了阻塞,可以对比不同执行结果 ...
 - 那些年我们一起追逐的多线程(Thread、ThreadPool、委托异步调用、Task/TaskFactory、Parallerl、async和await)
		
一. 背景 在刚接触开发的头几年里,说实话,根本不考虑多线程的这个问题,貌似那时候脑子里也有没有多线程的这个概念,所有的业务都是一个线程来处理,不考虑性能问题,当然也没有考虑多线程操作一条记录存在的并 ...
 
随机推荐
- SpringCloud之Eureka 服务注册和服务发现基础篇2
			
上篇文章讲解了SpringCloud组件和概念介绍,接下来讲解一下SpringCloud组件相关组件使用.原理和每个组件的作用的,它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix ...
 - mysql  datetime与timestamp精确到毫秒的问题
			
CREATE TABLE `tab1` (`tab1_id` VARCHAR(11) DEFAULT NULL,`create` TIMESTAMP(3) NULL DEFAULT NULL,`cre ...
 - 在Eclipse中配置Maven插件
			
--------------------------siwuxie095 在 Eclipse 中配置 Maven 插件 ...
 - TZOJ 2755 国际象棋(广搜+哈希)
			
描述 在n*n的国际象棋棋盘中,给定一“马(Knight)”和一“后(Queen)”的位置,问“马”能否在m步之内(包括m步)到达“后”的位置?马的走法是:每步棋先横走或直走一格,然后再斜走一格,即走 ...
 - 300最长上升子序列 · Longest Increasing Subsequence
			
[抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...
 - 133克隆图 · Clone Graph
			
[抄题]: 克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors. [思维问题]: [一句话思路]: 先BFS克隆点(一个点+扩展所有邻居),再克隆邻居(一个点+扩展所有 ...
 - Linux-——grep
			
概念介绍 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...
 - WCF生成客户端代理对象的两种方法的解释
			
最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...
 - noi 1997 最优乘车
			
H城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士站出发,依次途经若干个巴士站,最终 ...
 - python性能监控初试
			
标 题: python性能监控初试作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990765.html 欢迎转帖 请保持文本完整并注明出处 之前性能统计 ...