最近在学习 dart 的 isolate ,自己写了一段,没有输出…… 麻烦大佬们帮看下问题出在哪里?代码如下:
import 'dart:isolate';
/// isolate06_my_test.dart
/// 抽象一个类来存储自己的SendPort和要传递的消息,每次发送消息时都使用这个类来包装消息
///
///
/// [MessageWrap] 包装了消息和端口
class MessageWrap<T> {
final SendPort sendPort;
final T message;
final int count;
MessageWrap(this.sendPort, this.message, this.count);
}
/// main
main() async {
int num = 1;
ReceivePort mainReceivePort = ReceivePort();
await Isolate.spawn(topNewIsolateFun, mainReceivePort.sendPort);
mainReceivePort.listen((wrap){
if (!wrap is MessageWrap) throw Exception("消息类型传输错误!");
wrap = wrap as MessageWrap;
final msg = wrap.message.toString();
print("Main Isolate 接收到的消息: ${msg}");
final count = wrap.count;
if (num == 5) {
mainReceivePort.close();
} else {
print(wrap.message.toString());
wrap.sendPort.send(MessageWrap(mainReceivePort.sendPort,
"${count+1} : Main Isolate 向您问好!", count+1));
}
});
}
topNewIsolateFun(SendPort sendPort) async {
ReceivePort localReceivePort = ReceivePort();
localReceivePort.listen((wrap){
if (!wrap is MessageWrap) throw Exception("消息类型传输错误!");
wrap = wrap as MessageWrap;
final msg = wrap.message.toString();
print("New Isolate 接收到的消息: ${msg}");
final count = wrap.count;
if (msg == 'q') {
localReceivePort.close();
} else {
print(wrap.message.toString());
wrap.sendPort.send(MessageWrap(localReceivePort.sendPort,
"${count+1} : New Isolate 向您问好!", count+1));
}
});
}
=== add at 2022年1月24日 22:52:01 ===
再次打开电脑审视代码,发现 topNewIsolateFun
方法中的参数 sendPort
没有被使用过;也就是子isolate没有向main isolate发送过消息……
而且,代码中的类型判断也有问题。 必须是这样的: MessageWrap<String>
。
修改后的代码如下(可以正常运行):
import 'dart:isolate';
/// isolate06_my_test2.dart
///
///
/// [MessageWrap] 包装了消息和端口
class MessageWrap<T> {
final SendPort sendPort;
final T message;
final int count;
MessageWrap(this.sendPort, this.message, this.count);
}
/// main
main() async {
ReceivePort mainReceivePort = ReceivePort();
await Isolate.spawn(topNewIsolateFun, mainReceivePort.sendPort);
mainReceivePort.listen((wrap){
bool flag = wrap is MessageWrap<String>;
if (!flag) throw Exception("消息类型传输错误!");
//wrap = wrap as MessageWrap;
//final msg = wrap.message.toString();
//print("Main Isolate 接收到的消息: ${msg}");
final count = wrap.count;
if (count == 5) {
mainReceivePort.close();
} else {
print(wrap.message.toString());
wrap.sendPort.send(MessageWrap(mainReceivePort.sendPort,
"${count+1} : Main Isolate 向您问好!", count+1));
}
});
}
topNewIsolateFun(SendPort sendPort) async {
ReceivePort localReceivePort = ReceivePort();
localReceivePort.listen((wrap){
print(">> wrap's runtimeType is : ${wrap.runtimeType}");
bool flag = wrap is MessageWrap<String>;
//print("flag: $flag");
if (!flag) throw Exception("消息类型传输错误!");
//wrap = wrap as MessageWrap;
final msg = wrap.message.toString();
//print("New Isolate 接收到的消息: ${msg}");
final count = wrap.count;
if (msg == 'q') {
localReceivePort.close();
} else {
print(wrap.message.toString());
wrap.sendPort.send(MessageWrap(localReceivePort.sendPort,
"${count+1} : New Isolate 向您问好!", count+1));
}
});
sendPort.send(MessageWrap(localReceivePort.sendPort, "${1} : New Isolate 向您问好!", 1));
}