你可以 运行 Isolate.spawn 多次吗?

can you run Isolate.spawn multiple times?

这是我用来学习isolate的简单代码,我spawn了两次,但是第二次spawn没有显示任何东西,这里有什么错误吗?谢谢

import 'dart:isolate';
Future<void> main() async {
  print('start');
  await Isolate.spawn(echo, 'Dart');
  await Isolate.spawn(echo, 'Flutter'); // why this 2nd spawn not showing up?
  print('end');
}

void echo(msg) {
  print(msg);
}

您的程序在 Isolate 完成其工作之前退出。如果您添加

,您可以确认这一点
await Future.delayed(Duration(seconds: 1));

你的程序即将结束。

设置 Isolates 通常有点挑战,因为所有 SendPort 东西等等。