关于执行顺序和线程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. 「CodeM」排列

    传送门 Description 给 \(n\) 个二维点 \((a_i,b_i)\),询问有多少种排列 \(p\)(答案对 \(10^9+7\) 取模)使得执行以下伪代码后留下的点是 \(i\),即最 ...

  2. mac下编程使用字体

    1.xcode下使用的是Menlo的18号字体 2.webStorm使用的也是Menlo的18号字体 3.sublime Text 使用的也是Menlo的21号字体

  3. DDD(Domain Driven Design) 架构设计

    一.为什么要分层 分层架构是所有架构的鼻祖,分层的作用就是隔离,不过,我们有时候有个误解,就是把层和程序集对应起来,就比如简单三层架构中,在你的解决方案中,一般会有三个程序集项目:XXUI.dll.X ...

  4. shell(三)if流程控制

    流程控制 if 流程控制 基本语法 单分支结构 if [];then fi 两分支结构 if [];then else fi 多分支结构 if [];then elif[];then elif[];t ...

  5. Shell脚本之六 数学计算

    前面一节Shell篇之五 基本运算符介绍了常见的 Shell 算术运算符,这节介绍 Shell 的数学计算.Shell 和其它编程语言不同,Shell 不能直接进行算数运算,必须使用数学计算命令. 下 ...

  6. storm并行

    Storm并行度 wordcount 统计job代码 public class WordCountTopology { private static final String SENTENCE_SPO ...

  7. python入门之函数对象

    目录 函数是第一类对象 1.函数名可以被引用 2.函数名可以当做参数传递 3.函数名可以当做返回值使用 4.函数名可以被当做容器类型的元素 函数是第一类对象 First-Class Object : ...

  8. SSM 整合配置

    目录 1. Maven : pox.xml 2. Web container : web.xml 3. Spring context : dbconfig.properties + applicati ...

  9. 『Tree nesting 树形状压dp 最小表示法』

    Tree nesting (CF762F) Description 有两个树 S.T,问 S 中有多少个互不相同的连通子图与 T 同构.由于答案 可能会很大,请输出答案模 1000000007 后的值 ...

  10. Vue传递方法给页面调用

    很多人在使用vue的时候苦于在vue中写方法,但是在外部甚至在另一个js该如何调用呢? 这个方法就是显示了vue的可以传递方法到页面使得页面任何地方都可以调用 前提得引用文件 这个方法一般多用于加载周 ...