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)
一. 背景 在刚接触开发的头几年里,说实话,根本不考虑多线程的这个问题,貌似那时候脑子里也有没有多线程的这个概念,所有的业务都是一个线程来处理,不考虑性能问题,当然也没有考虑多线程操作一条记录存在的并 ...
随机推荐
- Mysql delete操作
以下摘自官方文档:https://dev.mysql.com/doc/refman/5.7/en/insert.html 语法: DELETE [LOW_PRIORITY] [QUICK] [IGNO ...
- Django入门-框架目录介绍
Django入门博客:https://www.cnblogs.com/chuangming/p/9076721.html#4098510 备注:使用 Django 框架之后,开发服务端方便了很多.我们 ...
- Json解析数据的简单使用
简单的记一下Json解析的简单实用: 使用场景:后台传到客户端的Json数据,类似于: string jsonObject="{'Name':'Jack','Age':25}"; ...
- 安装Ubuntu后一些准备
一些基础 安装的时候,先不选镜像就可以避开简易安装. 更改root密码:sudo passwd root 更改源,更新,不行就打断在更新 安装vim 改为unity模式,安装VMware Tools, ...
- Mybatis的分页插件PageHelp:Page对象中的pageSize等属性无法序列化,无法转换为json字符串
Page<User> page = new Page<>(); User user = new User(); user.setAge(20); ...
- 遍历XML文件
#encoding=utf-8 from xml.etree import ElementTree as ET #要找出所有人的年龄 per=ET.parse('d:\\1.xml') p=per.f ...
- js variable 变量
局部作用域 由于JavaScript的变量作用域实际上是函数内部,我们在for循环等语句块中是无法定义具有局部作用域的变量的: 'use strict'; function foo() { for ( ...
- centos7下mysql5.6的主从复制
一.mysql主从复制介绍 mysql的主从复制并不是数据库磁盘上的文件直接拷贝,而是通过逻辑的binlog日志复制到要同步的服务器本地,然后由本地的线程读取日志里面的sql语句,重新应用到mysql ...
- _GET_
_GET_:读取属性,没有自动创建
- DB2 create into的用法
. 建立表 create table zjt_tables as (select * from tables) definition only; create table zjt_views as ( ...