使用Isolate
的并发编程:与线程相似但不共享内存的独立区域,仅经过消息进行通讯。编程
要使用Isolate
进行并发编程:bash
import 'dart:isolate';
复制代码
该Library主要包含下面几个类:并发
Isolate
:Dart执行上下文的隔离区。ReceivePort
:与SendPort
一块儿,是隔离区之间惟一的通讯方式。SendPort
:将消息发送到其余ReceivePort
。全部Dart代码都在隔离区中运行,代码只能访问同一个隔离区的类和值。不一样的隔离区能够经过端口发送信息进行通讯。async
由spawn
操做提供的Isolate
对象将具备控制隔离所需的控制端口和功能。若有必要,可使用Isolate.Isolate
构造函数建立新的隔离对象,而无需使用这些功能。函数
要建立Isolate
,你可使用spawn
方法。必须为spawn
方法提供一个带有单个参数的“入口点”方法。 一般,此参数表示隔离应用于发送回通知消息的端口:ui
import 'dart:async';
import 'dart:isolate';
main(List<String> args) {
start();
print("start");
}
Isolate isolate;
int i =1;
void start()async{
//接收消息的主Isolate的端口
final receive = ReceivePort();
isolate = await Isolate.spawn(runTimer, receive.sendPort);
receive.listen((data){
print("Reveive : $data ; i :$i");
});
}
void runTimer(SendPort port){
int counter =0 ;
Timer.periodic(const Duration(seconds: 1), (_){
counter++;
i++;
final msg = "nitification $counter";
print("Send :$msg ;i :$i");
port.send(msg);
});
}
复制代码
运行上面的例子控制台的输出为:spa
start
Send :nitification 1 ;i :2
Reveive : nitification 1 ; i :1
Send :nitification 2 ;i :3
Reveive : nitification 2 ; i :1
Send :nitification 3 ;i :4
Reveive : nitification 3 ; i :1
复制代码
在上面的例子中,使用start
方法来建立一个端口并生成一个Isolate
。spawn
使用了两个参数,一个要执行的回调的函数runTimer
,和一个端口(SendPort
),回调能够用来将消息发送回调用者。端口是你与Isolate
通讯的方式。这个例子将其设置为单向通行(从隔离区发送信息到接收者),但它能够是双向的。在start
方法的最后是监听来自定义isolate
的消息。线程
在控制台的输出中能够看出,两个Isolate
中打印的i的值并不一致,则说明Isolate
之间的内存并非共享的。code
在上面的例子中,Isolate
能够一直进行下去。若是想Isolate
中止运行,可使用kill
方法。对象
void stop(){
print("kill isolate");
isolate?.kill(priority: Isolate.immediate);
isolate =null;
}
复制代码
上面的代码会杀死正在运行的Isolate
,并将引用置为null
。 Isolate.immediate
的优先级将在最近的机会干净地关闭Isolate
。
还能够暂停和恢复Isolate
的运行,分别对应Isolate
中的pause
方法和resume
方法。