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 ...
随机推荐
- 【Maven插件】exec-maven-plugin
<plugin> <artifactId>exec-maven-plugin</artifactId> <groupId>org.codehaus.mo ...
- jquery swipper插件的一些弊端
jquery swipper插件的一些弊端touch触摸机制是swipper的 阻止click冒泡.拖动Swiper时阻止click事件.下面这个方法或许可以解决触摸机制的问题 <pre> ...
- 什么是 ZFS文件系统?ZFS概念及特点简介
什么是 ZFS? ZFS(Zettabyte File System)是由SUN公司的Jeff Bonwick领导设计的一种基于Solaris的文件系统,最初发布于20014年9月14日. SUN被O ...
- | C语言I作业02
C语言I博客作业02 标签: 18软件2班 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/C ...
- 谷歌浏览器扩展程序中安装vue-devtools插件
1.下载vue-devtools插件 地址https://github.com/vuejs/vue-devtools 2.进入刚刚下载文件的目录下(最好路径中没有中文) npm install 再执行 ...
- Kafka Consumer Lag Monitoring
Sematext Monitoring 是最全面的Kafka监视解决方案之一,可捕获约200个Kafka指标,包括Kafka Broker,Producer和Consumer指标.尽管其中许多指标很 ...
- 概率dp - Uva 10900 So you want to be a 2n-aire?
So you want to be a 2n-aire? Problem's Link Mean: 玩一个答题赢奖金的游戏,一开始有1块钱,玩n次,每次赢的概率为t~1之间的某个实数. 给定n和t,求 ...
- TYPORA语法
原文链接:https://blog.csdn.net/SIMBA1949/article/details/79001226
- 【chromium】 Chromium OS的oom机制
前一段时间,运行在Chromium OS上的一个相机应用经常会自己崩溃,进程戛然而止,测试过程中发现使用的内存以肉眼可见的内存增长,当增长到1G左右,应用窗口突然消失,虽然原因不明,但是能猜到个大概, ...
- Java中Integer和ThreadLocal
一. Integer 1.引子 在开始之前,我还是需要吐槽下自己,我是真的很菜! 他问:**两个Integer对象==比较是否相等? 我答:对象==比较,是引用比较,不相等! 他问:IntegerCa ...