Isolate 隔离运行
import 'dart:async';
import 'dart:isolate';
/// 隔离处理函数
/// 顶级函数或类的静态方法
void handler(SendPort sendPort)
{
sendPort.send('I am Tom: '+sendPort.hashCode.toString());
}
void main()
{
var receivePort = ReceivePort();
var sendPort = receivePort.sendPort;
await Isolate.spawn<SendPort>(handler, sendPort);
sendPort.send('I am Gim!'+sendPort.hashCode.toString());
//方式一 事件流订阅者
receivePort.listen((target){
print('Receive message: '+target.toString());
});
// 方式二 绑定处理函数
receivePort.forEach((target){
print('Receive message: '+target.toString());
});
// 方式三 流的方式读取
await for(var target in receivePort) {
print('Receive message: '+target.toString());
}
}
网友评论