关于执行顺序和线程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执行顺序的更多相关文章

  1. async/await 执行顺序详解

    随着async/await正式纳入ES7标准,越来越多的人开始研究据说是异步编程终级解决方案的 async/await.但是很多人对这个方法中内部怎么执行的还不是很了解,本文是我看了一遍技术博客理解 ...

  2. 错误的理解引起的bug async await 执行顺序

    今天有幸好碰到一个bug,让我知道了之前我对await async 的理解有点偏差. 错误的理解 之前我一直以为  await 后面的表达式,如果是直接返回一个具体的值就不会等待,而是继续执行asyn ...

  3. setTimeout,promise,promise.then, async,await执行顺序问题

    今天下午看了好多关于这些执行顺序的问题  经过自己的实践 终于理解了  记录一下就拿网上出现频繁的代码来说: async function async1() { console.log('async1 ...

  4. promise.then, setTimeout,await执行顺序问题

    promise.then VS setTimeout 在chrome和node环境环境中均输出2, 3, 1, 先输出2没什么好说的,3和1顺序让人有些意外 原因: 有一个事件循环,但是任务队列可以有 ...

  5. javascript中的defer和async学习+javascript执行顺序

    一.defer和async 我们常用的script标签,有两个和性能.js文件下载执行顺序相关的属性:defer和async defer的含义[摘自https://developer.mozilla. ...

  6. 事件循环 EventLoop(Promise,setTimeOut,async/await执行顺序)

    什么是事件循环?想要了解什么是事件循环就要从js的工作原理开始说起: JS主要的特点就是单线程,所谓单线程就是进程中只有一个线程在运行. 为什么JS是单线程的而不是多线程的呢? JS的主要用途就是与用 ...

  7. Promise嵌套问题/async await执行顺序

    /* 原则: 执行完当前promise, 会把紧挨着的then放入microtask队尾, 链后面的第二个then暂不处理分析, */ 一. new Promise((resolve, reject) ...

  8. Node async 控制代码执行顺序

    当你有一个集合,你想循环集合,然后对每个集合按照顺序执行相应的方法你可以使用forEachSeries

  9. promise、async、await、settimeout异步原理与执行顺序

    一道经典的前端笔试题,你能一眼写出他们的执行结果吗? async function async1() { console.log("async1 start"); await as ...

随机推荐

  1. [300iq Contest 1-D]Dates

    传送门 Description 每个妹子匹配一个时间区间,每个时间最多选择\(a_i\)个妹子,每个妹子有一个快乐值,最大化总快乐值 Solution 贪心,从大往小取,能取则取 判断是否可以有完美匹 ...

  2. 对post提交数据Content-Type的理解

    Content-Type是指http/https发送信息至服务器时的内容编码类型,contentType用于表明发送数据流的类型,服务器根据编码类型使用特定的解析方式,获取数据流中的数据. 在网络请求 ...

  3. Redis与Mysql双写一致性方案解析

    一 前言 首先,缓存由于其高并发和高性能的特性,已经在项目中被广泛使用.在读取缓存方面,大家没啥疑问,都是按照下图的流程来进行业务操作 但是在更新缓存方面,对于更新完数据库,是更新缓存呢,还是删除缓存 ...

  4. Spring Security教程之session管理(十一)

    1.1     检测session超时 1.2     concurrency-control 1.3     session 固定攻击保护 Spring Security通过http元素下的子元素s ...

  5. [技术博客]ubuntu+nginx+uwsgi+Django+https的部署

    ubuntu+nginx+uwsgi+Django+https部署文档 配置机器介绍 操作系统:Ubuntu 18.04.2 LTS 64位 python版本:Python 3.6.7 Django版 ...

  6. ORM之Dapper

    ORM之Dapper 一.下载安装: nuget 搜索dapper安装 二.使用: 三.优缺点: 优点: 1.开源.轻量.单文件(代码就一个SqlMapper.cs文件,编译后就40K的一个很小的Dl ...

  7. 自定义Yaml解析器替换Properties文件

    自定义Yaml解析器替换Properties文件 项目结构 案例代码 配置类SpringConfiguration @Configuration @Import(JdbcCofnig.class) @ ...

  8. MySQL 索引小结

    1.!=.not in 在primary key上使用 !=.not in,explain 的 type 是 range,非primary key是全表扫描(即非主键字段即使有索引也无法应用) 2.a ...

  9. 【转】2019年7月份,阿里最新Java高频面试真题汇总

    技术一面(23问)技术二面(3大块)JAVA开发技术面试中可能问到的问题(17问)JAVA方向技术考察点(33快)项目实战(7大块)必会知识(48点)面试小技巧注意事项1. 阿里技术一面 Java I ...

  10. 上传文件时用form.submit提交的时候在低版本的IE中报拒绝访问的错误

    上传文件的时候,在IE7下总是传不了,但FireFox,IE11和Chrome下则可以上传.发现是form.submit();时出错了(“拒绝访问”). html代码为: <label oncl ...