zoukankan      html  css  js  c++  java
  • Flutter main future mirotask 的执行顺序

    下面这段代码的输出是什么?

    import 'dart:async';
    main() {
      print('main #1 of 2');
      scheduleMicrotask(() => print('microtask #1 of 2'));
     
      new Future.delayed(new Duration(seconds:1),
                         () => print('future #1 (delayed)'));
      new Future(() => print('future #2 of 3'));
      new Future(() => print('future #3 of 3'));
     
      scheduleMicrotask(() => print('microtask #2 of 2'));
     
      print('main #2 of 2');
    }

     答案在下面:

    1 main #1 of 2
    2 main #2 of 2
    3 microtask #1 of 2
    4 microtask #2 of 2
    5 future #2 of 3
    6 future #3 of 3
    7 future #1 (delayed)

    main方法中的普通代码都是同步执行的,所以肯定是main打印先全部打印出来,等main方法结束后会开始检查microtask中是否有任务,若有则执行,执行完继续检查microtask,直到microtask列队为空。所以接着打印的应该是microtask的打印。最后会去执行event队列(future)。由于有一个使用的delay方法,所以它的打印应该是在最后的。

    原文:https://blog.csdn.net/meiyulong518/article/details/81773365 

  • 相关阅读:
    ASP.NET中级学习3
    C#面向对象学习笔记
    Javascript学习笔记
    FormView控件使用
    ASP.NET初级学习
    ListView控件是使用
    Java NIO 学习笔记一
    堆栈和托管堆 c#
    安装php7.2并且整合nginx且分开部署
    Python 安装requests和MySQLdb
  • 原文地址:https://www.cnblogs.com/yangyxd/p/10411114.html
Copyright © 2011-2022 走看看