美文网首页
Isolate线程通信

Isolate线程通信

作者: 温水煮青蛙a | 来源:发表于2023-03-10 20:56 被阅读0次

Isolate.spawn 创建形式

子线程返回数据到主线程

  • 主线程不需要回传数据给子线程
void function_main() async {
    print("当前线程:"+ Isolate.current.debugName);
    // 声明ReceivePort对象用来监听回调的数据
    var receivePort = new ReceivePort();
    // 两个isolate之间通过SendPort来进行通信
    Isolate childIsolate = await Isolate.spawn(entryPoint, "就是entryPoint方法中接受的参数");
    /* 接收方式 1 */
    // 意思是第一次接收到消息,msg 就是子线程发送过来的数据
    String msg = await receivePort.first;
    
    /* 接收方式 2 */
    receivePort.listen((message) {
      //msg 就是子线程发送过来的数据
      String msg = message;
      // 关闭监听
      receivePort.close();
      //杀死线程资源
      childIsolate.kill();
    });
  }
  void entryPoint(String str) async {
    // 这里把回传的数据send回主线程
    sendPort.send("将数据发送到主线程 + ${str}");
  }

子线程和主线程之间通信

  • 子线程返回数据到主线程后-主线程再次返回数据到子线程
void function_main() async {
    print("当前线程:"+ Isolate.current.debugName);
    // 声明ReceivePort对象用来监听回调的数据
    var receivePort = new ReceivePort();
    // 两个isolate之间通过SendPort来进行通信
    await Isolate.spawn(entryPoint, receivePort.sendPort);
    /* 接收方式 1 */
    // 意思是第一次接收到消息
    SendPort sendPort = await receivePort.first;
    // 用子线程发送过来的SendPort对象-发送数据,-》子线程就会收到通知
    sendPort.send("回复给子线程的内容");
    /* 接收方式 2 */
    receivePort.listen((message) {
      SendPort sendPort = message;
      // 用子线程发送过来的SendPort对象-发送数据,-》子线程就会收到通知
      sendPort.send("回复给子线程的内容");
      // 关闭监听
      receivePort.close();
    });
  }
  void entryPoint(SendPort sendPort) async {
    // 创建跟当前线程关联的ReceivePort对象
    var port = new ReceivePort();
    // 把SendPort对象发送到主线程
    sendPort.send(port.sendPort);
    // 这里是接收到 主线程给回复的内容
    await for (var data in port) {
      print("data $data");
    }
  }

Isolate.spawnUri 创建形式

mainIsolate.dart文件中

import 'dart:isolate';

main() {
  //启动应用
  start();
  //创建新线程
  newIsolate();
  //项目初始化
  init();
}

void start(){
  print("启动应用" + DateTime.now().microsecondsSinceEpoch.toString());
}

void newIsolate() async {
  print("创建新线程");
  ReceivePort r = ReceivePort();
  SendPort p = r.sendPort;
  //创建新线程
  Isolate childIsolate = await Isolate.spawnUri(
      Uri(path: "childIsolate.dart"),//这里引用的是文件路径
      ["data1", "data2", "data3"],//这里传参就是文件方法中接收到args
      p);//这里的p就是文件方法中接收的SendPort对象
  r.listen((message) {
    print("主线程接收到数据:" + message[0]);
    if (message[0] == 0){
      //异步任务开始
    } else if (message[1] == 1){
      //异步任务正在处理
    } else if (message[2] == 2){
      //异步任务完成
      //取消监听
      r.close();
      //杀死新线程,释放资源
      childIsolate.kill();
    }
  });
}

void init() {
  print("项目初始化");
}

childIsolate.dart文件中

import 'dart:io';
import 'dart:isolate';

main(List<String> args, SendPort mainSendPort) {
  print("新线程接收到的参数:$args");
  //发送数据到主线程
  mainSendPort.send(["开始异步操作", 0]);
  //延时3秒
  sleep(Duration(seconds: 3));
  //发送数据到主线程
  mainSendPort.send(["加载中", 1]);
  //延时3秒
  sleep(Duration(seconds: 3));
  //发送数据到主线程
  mainSendPort.send(["异步任务完成", 2]);
}

相关文章

  • Futter 练习 Isolate

    简介 Dart 提供了 isolate, isolate与线程类似,isolate与线程的区别是 :线程与线程之间...

  • flutter-isolate详解

    一. isolate简介 Dart 是单线程,Dart 为我们提供了 isolate,isolate 跟线程差不多...

  • 跟我学flutter:我们来举个例子通俗易懂讲解异步(一)ios

    前言 Dart是单线程的,Dart提供了Isolate,isolate提供了多线程的能力。但作为多线程能力的,却内...

  • Isolate

    1、(Isolate)隔离的定义:类似于线程(区别就是Isolate数据不能通用,只能把数据传回主Isolate,...

  • 网络与项目实战

    异步多线程结合 isolate与进程间的区别 isolate除了拥有线程,还有独立的内存空间,但这个内存空间是局部...

  • Flutter之-dart多线程isolate(二)

    dart中的isolate isolate可以理解为dart中的线程,但它又不同于线程,准确的说应该叫做协程,协程...

  • 4.Dart多线程---Isolate

    Dart多线程---Isolate isolate是Dart对actor并发模式的实现。运行中的Dart程序由一个...

  • Flutter关于Isolate的初步学习应用

    Isolate 在Dart中实现并发可以用Isolate,它是类似于线程(thread)但不共享内存的独立运行的w...

  • Flutter 之 Isolate

    在Dart中实现并发可以使用Isolate。Isolate翻译过来是孤立、隔离的意思。它是类似于线程但不共享内存的...

  • Isolate

    在Dart中并发是通过Isolate来实现的。Isolate和线程的区别是它不共享内存。Dart VM中采用了多生...

网友评论

      本文标题:Isolate线程通信

      本文链接:https://www.haomeiwen.com/subject/keptrdtx.html