关于执行顺序和线程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. UDF——监测指定点的物理量

    Fluent版本:2019 R1 Visual Studio版本:Visual Studio 2013 其他版本应该也是适用的 算例来源于:https://confluence.cornell.edu ...

  2. Go:为何带来泛型

    中文版 English version 介绍 [这是在Gophercon 2019上发表的演讲版本.视频链接可供使用.] 这篇文章是关于向Go添加泛型的意义,以及为什么我认为我们应该这样做.我还将介绍 ...

  3. 【Gamma阶段】第九次Scrum Meeting

    冰多多团队-Gamma阶段第九次Scrum会议 工作情况 团队成员 已完成任务 待完成任务 卓培锦 美化前端及编辑器界面,编辑器风格切换(添加夜间模式) UI界面手势切换 牛雅哲 添加scp工具,添加 ...

  4. Elasticsearch SQL用法详解

    Elasticsearch SQL用法详解  mp.weixin.qq.com 本文详细介绍了不同版本中Elasticsearch SQL的使用方法,总结了实际中常用的方法和操作,并给出了几个具体例子 ...

  5. C#】通过遍历IFrame访问页面元素

    最近在做一个小项目,期间需要用到C#去操作IE页面中的元素,实现自动填写表单并且提交的功能,想这网上关于这方面的东西肯定很多,于是开始在网上找资料. 在逆心的博客上找到些东西对自己帮助很大,原文链接: ...

  6. 设计高性能大并发WEB系统架构注意点

    设计高性能大并发WEB系统架构注意点 第01:大型架构的演进之路第02(上):分布式缓存第02(下):分布式缓存第03:分布式消息队列第04:分布式数据存储第05:分布式服务框架第06:高性能系统架构 ...

  7. idea git使用记录

    ps:记录idea中git的使用过程 在IntelliJ IDEA使用.gitignore插件的方法和作用 在IntelliJ IDEA使用.gitignore插件的方法和作用 idea git st ...

  8. 网关/负载均衡下的consul集群代理

    之前有做过使用单机版的consul实现Prometheus服务注册,以为使用集群版的consul只是将consul服务地址从节点IP变为了网关IP.但比较坑的就是,当使用consul注册一个servi ...

  9. Python3多进程共享变量实现方法

    今天同事反映一个问题让帮忙看一下:多进程共用一个变量,在一个进程中修改后,在另外的进程中并没有产生修改. 一.错误的实现方式 最初以为是没添加global声明导致修改未生效,但实际操作发现global ...

  10. Lua代码编写规范

    开发中,大量使用lua,暂时根据当前状况,总结相对而言较好的规范,在多人协作中可以更好的开发.交流. 介绍  该文档旨在为使用lua编写应用程序建立编码指南. 制订编码规范的目的: 统一编码标准,通用 ...