Flutter Isolate 隔离的使用

创建Isolate 的两种方法:

Isolate.spawn[dart里面的] 和 compute()[flutter里面的]

上面的两个函数使用方法和传入的参数方式都一样,一个是dart里面的一个是flutter里面的。

第一个参数:静态方法,或顶级函数

第二个参数:可以传入任何类型的值,比如官方的:List Map String int bool

Isolate.spawn((message) {
  //打印传入的参数
  print(message);
}, '要传入的参数');

Isolate 单项通信:

//创建单向通信隔离
  Future<void> createOneWayCommunicationIsolation() async {
    //打印下当Isolate的名称 //main
    print(Isolate.current.debugName);
    //创建接收端口
    final ReceivePort receivePort = ReceivePort();
    //创建发送端口
    SendPort sendport = receivePort.sendPort;

    //接收新新线程发来的消息[两种监听方法只能同时使用一种]
    //第一种监听方法
    receivePort.listen((message) {
      print(message); // 这是发给主线程的消息
    });
    //第二种监听方法
    // var msg = receivePort.first;

    //创建隔离 【第二个参数是,把创建的发送端口传过去】
    Isolate.spawn((sendport) {
      //打印下当Isolate的名称
      print(Isolate.current.debugName); //自定义隔离名称

      //发送消息给主线程
      sendport.send('这是发给主线程的消息');
    }, sendport, debugName: '自定义隔离名称');
  }

Isolate 双向通信:

  //创建双向通信隔离
  void createTwoWayCommunicationIsolation() {
    //打印下当Isolate的名称 //main
    print(Isolate.current.debugName);
    //创建接收端口
    final ReceivePort r1 = ReceivePort();
    //创建发送端口
    SendPort s1 = r1.sendPort;

    //接收新新线程发来的消息[两种监听方法只能同时使用一种]
    //第一种监听方法
    r1.listen((message) {
      SendPort s = message[0];
      String d = message[1];
      print('接收到新线程发来的消息:$d'); //接收到新线程发来的消息:这是发给主线程的消息
      s.send('这是发给子线程的消息');
    });
    //第二种监听方法
    // var msg = receivePort.first;

    ///创建隔离 【第二个参数是,把创建的发送端口传过去】
    Isolate.spawn((sendport) {
      //打印下当Isolate的名称
      print(Isolate.current.debugName); //自定义隔离名称

      // 创建接收端口
      final ReceivePort r2 = ReceivePort();
      //创建发送端口
      SendPort s2 = r2.sendPort;

      ///监听主线程发来的消息
      r2.listen((message) {
        print('接收到主线程发来的消息:$message'); //接收到主线程发来的消息:这是发给子线程的消息
      });

      ///把新线程创建的发送端口传给主线程
      sendport.send([s2, '这是发给主线程的消息']);
    }, s1, debugName: '自定义隔离名称');
  }

IsolateNameServer.registerPortWithName 和 IsolateNameServer.lookupPortByName 的使用:

 static Future<void> createIsolate() async {
    //创建接收端口
    ReceivePort receivePort = ReceivePort();
    //监听接收端口
    receivePort.listen((message) {
      print('隔离返回的数据:$message');
      //隔离返回的数据:主线程发送给接收端口的数据
      //隔离返回的数据:这是新线程发送给主线程接收端口的数据
    });

    //通过名称注册发送端口
    IsolateNameServer.registerPortWithName(receivePort.sendPort, 'zhao');

    //通过昵称查找发送端口,然后发送数据
    IsolateNameServer.lookupPortByName('zhao')?.send('主线程发送给接收端口的数据');

    //创建隔离
    Isolate.spawn((message) {
      //通过昵称查找发送端口,然后发送数据
      IsolateNameServer.lookupPortByName(message)?.send('这是新线程发送给主线程接收端口的数据');
    }, 'zhao');

    //删除注册的发送端口
    IsolateNameServer.removePortNameMapping('zhao');
  }

其他方法:

    ReceivePort receivePort = ReceivePort();
    //关闭接收端口。
    // 接收端口将不再接收任何事件,也不会作为流事件发出。
    // 如果已调用listen且StreamSubscription尚未取消,则订阅将通过“完成”事件关闭。
    // 如果流已被取消,则此方法无效
    receivePort.close();
    //将消息发送到此接收端口的SendPort 。
    SendPort s = receivePort.sendPort;
    //发送消息
    s.send('message');
    
    
    //表示当前隔离的Isolate对象
    Isolate.current;
    //获取当前线程的名称
    Isolate.current.debugName;
    //获取获取当前线程的发送端口并发送消息
    Isolate.current.controlPort.send('message');
    //请求隔离关闭。
    // 隔离被请求终止自身。 priority参数指定何时必须发生。
    // priority在提供时必须是immediate或beforeNextEvent之一(默认值)。关闭根据优先级在不同时间执行:
    Isolate.current.kill();
    // 同步终止当前隔离。
    // 此操作具有潜在危险,应谨慎使用。隔离立即停止运行。
    Isolate.exit();
THE END
喜欢就支持一下吧
点赞7赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容