async和await执行顺序
关于执行顺序和线程ID,写了一个小程序来检测学习:
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks; namespace AsyncOrder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Main: befor Task1 。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
const string url1 = "http://www.163.com/";
const string url2 = "http://www.baidu.com/"; Task<string> t1 = p1(, url1);
Task<string> t2 = p1(, url2); DoSomeThing(, "main"); Console.WriteLine($"网页:{url1}长度:{t1.Result.Length}。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"网页:{url2}长度:{t2.Result.Length}。 线程ID:{Thread.CurrentThread.ManagedThreadId}"); DoSomeThing(, "main"); Console.WriteLine("按任意键退出...");
Console.ReadKey();
} private static async Task<string> p1(int id, string url = "http://www.baidu.com")
{
DoSomeThing(id,"P1");
Console.WriteLine($"任务ID:{id} ,p1: befor await。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
string ret = await p2(id,url);
Console.WriteLine($"任务ID:{id} ,p1: after await。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
return ret;
} private static async Task<string> p2(int id, string url = "http://www.baidu.com")
{
DoSomeThing(id, "P2");
Console.WriteLine($"任务ID:{id} ,p2: befor await。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
string ret = await GetWeb(id, url);
Console.WriteLine($"任务ID:{id} ,p2: after await。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
return ret;
} private static void DoSomeThing(int id, string v)
{
Console.WriteLine($"任务ID:{id} ,父程序:{v}。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
} private static async Task<string> GetWeb(int id , string url = "http://www.baidu.com")
{
var wc = new WebClient();
Console.WriteLine($"任务ID:{id} ,GetWeb: befor await wc。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
var temp = await wc.DownloadStringTaskAsync(url);
Console.WriteLine($"任务ID:{id} ,GetWeb: after await wc。 线程ID:{Thread.CurrentThread.ManagedThreadId}");
return temp.ToString();
}
}
}
执行结果为:

注意:有的时候任务1 2中的await之后的线程ID为同一个,不解。若有人明白,烦请解惑。
async和await执行顺序的更多相关文章
- async/await 执行顺序详解
随着async/await正式纳入ES7标准,越来越多的人开始研究据说是异步编程终级解决方案的 async/await.但是很多人对这个方法中内部怎么执行的还不是很了解,本文是我看了一遍技术博客理解 ...
- 错误的理解引起的bug async await 执行顺序
今天有幸好碰到一个bug,让我知道了之前我对await async 的理解有点偏差. 错误的理解 之前我一直以为 await 后面的表达式,如果是直接返回一个具体的值就不会等待,而是继续执行asyn ...
- setTimeout,promise,promise.then, async,await执行顺序问题
今天下午看了好多关于这些执行顺序的问题 经过自己的实践 终于理解了 记录一下就拿网上出现频繁的代码来说: async function async1() { console.log('async1 ...
- promise.then, setTimeout,await执行顺序问题
promise.then VS setTimeout 在chrome和node环境环境中均输出2, 3, 1, 先输出2没什么好说的,3和1顺序让人有些意外 原因: 有一个事件循环,但是任务队列可以有 ...
- javascript中的defer和async学习+javascript执行顺序
一.defer和async 我们常用的script标签,有两个和性能.js文件下载执行顺序相关的属性:defer和async defer的含义[摘自https://developer.mozilla. ...
- 事件循环 EventLoop(Promise,setTimeOut,async/await执行顺序)
什么是事件循环?想要了解什么是事件循环就要从js的工作原理开始说起: JS主要的特点就是单线程,所谓单线程就是进程中只有一个线程在运行. 为什么JS是单线程的而不是多线程的呢? JS的主要用途就是与用 ...
- Promise嵌套问题/async await执行顺序
/* 原则: 执行完当前promise, 会把紧挨着的then放入microtask队尾, 链后面的第二个then暂不处理分析, */ 一. new Promise((resolve, reject) ...
- Node async 控制代码执行顺序
当你有一个集合,你想循环集合,然后对每个集合按照顺序执行相应的方法你可以使用forEachSeries
- promise、async、await、settimeout异步原理与执行顺序
一道经典的前端笔试题,你能一眼写出他们的执行结果吗? async function async1() { console.log("async1 start"); await as ...
随机推荐
- c++开发遇到的错误和引用配置
1. libcurl引入的时候必须要加载下面三个库 #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "wl ...
- js追加html元素
jquery追加html代码,添加元素 .append() //新增仲裁申请人 $("."+inputName).append("<div class=\" ...
- C#】通过遍历IFrame访问页面元素
最近在做一个小项目,期间需要用到C#去操作IE页面中的元素,实现自动填写表单并且提交的功能,想这网上关于这方面的东西肯定很多,于是开始在网上找资料. 在逆心的博客上找到些东西对自己帮助很大,原文链接: ...
- 解决 ImportError: cannot import name 'initializations' from 'keras' (C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\keras\__init__.py)
解决 ImportError: cannot import name 'initializations' from 'keras' : [原因剖析] 上述代码用的是 Keras version: '1 ...
- Maven 教程(17)— Maven Profile 和 Filtering 简介
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79774572 每个项目都会有多套运行环境(开发,测试,正式等等),不同的环境配置 ...
- this与bind(this) (es5新增)
this与bind(this) this this指向的是当前函数的作用域(对象实例),有如下的例子 const app = { name: 'xiaoming', log() { console.l ...
- Haskell-chp01
-- 函数名首字母必须小写,可以包含 '来表示该函数严格求值版本(与惰性求值相对) doubleMe x = x + x doubleUs x y = doubleMe x + doubleMe y ...
- 30分钟用 Laravel 实现一个博客
介绍 Laravel 是一款 MVC架构. 目前最流行的 PHP框架. Laravel的优点在于: 丰富的composer类库支持, 优雅的代码, 未来的主流框架(目前市场占有率最高的框架) Lara ...
- IAR_STM32_CCM内存使用
在IAR中,硬件环境为STM32F464单片机,额外的CCM不能用DMA访问,但可以作为内部额外扩充RAM使用 修改文件中的内容为: define symbol __ICFEDIT_region_RO ...
- golang 学习笔记 -- 类型
int 和 uint的实际宽度会根据计算架构不同而不同,386下4个字节, amd64下8个字节 byte可看做uint8的别名类型 rune可看做int32的别名类型,专用于存储Unicode编码的 ...