Dart 中事件的执行顺序:Main > MicroTask > EventQueue
Dart app运⾏后默认创建的isolate为main isolate, 主程序的main⽅法就运⾏在这⾥。isolate创建时都会包含⼀个event loop和两个队列 event queue 和 microtask queue。
程序运⾏后main⽅法中的所有代码都是同步执⾏的,不会进⼊任何⼀个队列,main⽅法执⾏完之后event loop才会从microtask queue中取任务来执⾏,当microtask queue中任务都执⾏结束, event loop会去取event queue中的第⼀个任务来执⾏,执⾏完后会再次检查microtask queue中是否有任务。也就是说event queue每次执⾏⼀个任务,任务结束后会再次检查microtask queue中是否有待处理的任务,如果有,就会先 将microtask queue中的任务都处理完,再去event queue执⾏下⼀个。

new Future(() => print('Future==event queue 1'));
scheduleMicrotask((){
print("Microtask===1");
});
new Future(() => print('Future==event queue 2'));
scheduleMicrotask((){
print("Microtask===2");
});
new Future(() => print('Future==event queue 3'));
scheduleMicrotask((){
print("Microtask===3");
});
print('main');
// I/flutter (31521): main
// I/flutter (31521): Microtask===1
// I/flutter (31521): Microtask===2
// I/flutter (31521): Microtask===3
// I/flutter (31521): Future==event queue 1
// I/flutter (31521): Future==event queue 2
// I/flutter (31521): Future==event queue 3
多个Future
- 规则一:Future 的执行顺序为Future的在 EventQueue 的排列顺序。类似于 JAVA 中的队列,先来先执行。
- 规则二:当任务需要延迟执行时,可以使用 new Future.delay() 来将任务延迟执行。
- 规则三: Future 如果执行完才添加 then ,该任务会被放入 microTask,当前 Future 执行完会执行 microTask,microTask 为空后才会执行下一个Future。
- 规则四:Future 是链式调用,意味着Future 的 then 未执行完,下一个then 不会执行。
情况一:Future没有执行完成(有任务需要执行),那么then会直接被添加到Future的函数执行体后;
情况二:如果Future执行完后就then,该then的函数体被放到如微任务队列,当前Future执行完后执行微任务队列;
情况三:如果Future世链式调用,意味着then未执行完,下一个then不会执行;
Future f1 = new Future(() => print('f1'));
Future f2 = new Future(() => null);
Future f3 = new Future.delayed(Duration(seconds: 1) ,() => print('f3-delay'));
Future f4 = new Future(() => null);
Future f5 = new Future(() => null);
f5.then((_) => print('f5-then'));
f4.then((_) {
print('f4-then');
Future f6 = new Future(() => print('f4-then===f6'));
f2.then((_) {
print('f4-then==f2-then');
Future f7 = new Future(() => null);
Future f8 = new Future(() => print('f4-then===f2-then===f8'));
f7.then((value) => print('f4-then===f2-then===f7-then'));
});
});
f2.then((m) {
print('f2-then');
});
print('main==8');
// I/flutter (31521): main==8
// I/flutter (31521): f1
// I/flutter (31521): f2-then
// I/flutter (31521): f4-then
// I/flutter (31521): f4-then==f2-then
// I/flutter (31521): f5-then
// I/flutter (31521): f4-then===f6
// I/flutter (31521): f4-then===f2-then===f7-then
// I/flutter (31521): f4-then===f2-then===f8
// I/flutter (31521): f3-delay
例子打印分析:
- main==8
- f1 f2 f4 f5按顺序直接执行,f3 延时1s后执行 且属于 event queue,打印f1
- 按照Future加入的顺序执行then操作
- f1没有then操作; f2执行完添加then,该任务被加入microTask优先执行,打印f2-then
- f3有延时,且没有then操作; f4执行完添加then,该任务被计入microTask优先执行,打印f4-then
- f4 then中, f6加入event queue, f2执行完添加then,该任务被加入microTask优先执行,优先于f5的then 打印f4-then==f2-then
- f2 then执行完,f7 f8任务加入event queue
- f5执行完添加then,该任务被计入microTask优先执行,打印f5-then
- microTask中没有任务了,event queue 中f6 f7直接执行,打印f4-then===f6
- f7执行完添加then,该任务被计入microTask优先执行,打印f4-then===f2-then===f7-then
- microTask中没有任务了,f8直接执行,打印f4-then===f2-then===f8
- 1s后 microTask中没有任务了,f3直接执行,打印f3-delay
多个MicroTask + 多个Future
Future f1 = new Future(() => print('1'));
scheduleMicrotask(() => print('2'));
Future f3 = new Future.delayed(new Duration(seconds: 1), () => print('3'));
Future f4 = new Future(() => print('4'));
Future f5 = f4.then((_) {
scheduleMicrotask(() => print('5'));
new Future(() => print('6'));
print('7');
});
f5.then((_) => print('8'));
Future f6 = new Future(() => print('9'));
Future f7 = f6.then((_) {
scheduleMicrotask(() => print('10'));
new Future(() => print('11'));
print('12');
});
f7.then((_) => print('13'));
Future f8 = new Future(() => print('14'));
Future f9 = f8.then((_) =>new Future(() => print('15')));
f9.then((_) => print('16'));
new Future(() => print('17'));
scheduleMicrotask(() => print('18'));
print('19');
// I/flutter (11181): 19
// I/flutter (11181): 2
// I/flutter (11181): 18
// I/flutter (11181): 1
// I/flutter (11181): 4
// I/flutter (11181): 7
// I/flutter (11181): 8
// I/flutter (11181): 5
// I/flutter (11181): 9
// I/flutter (11181): 12
// I/flutter (11181): 13
// I/flutter (11181): 10
// I/flutter (11181): 14
// I/flutter (11181): 17
// I/flutter (11181): 6
// I/flutter (11181): 11
// I/flutter (11181): 15
// I/flutter (11181): 16
// I/flutter (11181): 3
按照以上思路,是否可以分析这段代码的打印结果?
网友评论