flutter实现并发方法

dart是单线程模型,并发操作通过Isolate实现

1. compute

flutter提供了高级APIcompute,自动管理Isolate、执行方法,在不复杂的应用场景中,使用compute方法可以帮助我们快速实现并发操作。

compute方法举例

先定义两个耗时方法

//定义两个耗时方法,每隔2秒打印一次,总共打印50次

  static void print1(String p1){
    for(int i = 0; i < 50; i++) {
      sleep(Duration(seconds: 2));
      print(p1);
    }
  }
  static void print2(String p2) {
    for(int i = 0; i < 50; i++) {
      sleep(Duration(seconds: 2));
      print(p2);
    }
  }

现在通过compute方法让两个耗时操作同时执行

  1. 使用compute把耗时方法转化成异步并发方法
 static void compute1(String c1) async{
   await compute(print1,c1);
  }

 static void compute2(String c2) async{
    await compute(print2,c2);
  }
  1. 调用两个异步方法
static void test(){
    compute1("c1");
    compute2("c2");
  }
  1. 查看打印日志可以看到两个方法同时打印


    截屏2022-08-13 11.41.30.png

2. Isolate

虽然compute方法可以帮助我们轻易的实现异步操作的需求,但是compute也是存在缺陷的,由于compute方法是由flutter控制管理Isolate,当我们面对复杂的使用场景时,频繁使用compute方法可能会造成大量Isolate的创建和销毁,这对性能损耗是比较大的。直接使用Isolate可以实现对之前创建的Ioslate的复用。

Isolate方法举例
  1. 创建IsolateIsolate之间用来通信的port
    // 通信用的port
    rep = ReceivePort();
   //  新建Isolate,print2是使用新Isolate调用的方法,传入的 rep.sendPort是当前
   //Isolate持有的ReceivePort的发送器,使新iosLate可以通过这个发送器给当前Isolate发信息
    iosLate = await Isolate.spawn(print2, rep.sendPort);
  1. 为rep添加监听方法,处理新Isolate发送过来的消息
rep.listen((message) {
      if(message is SendPort){
        //如果新的iosLate将它自己的ReceivePort的发送器发送过来,当前iosLate就可以通过
        //这个发送器给新iosLate发送信息,实现两个iosLate的双向通讯
        send2 = message;
     // 给新isolate发送消息
        send2.send("custom isolate print");
  // 测试是否可以异步操作,添加了一段耗时代码
        for(int i = 0; i < 50; i++) {
          sleep(Duration(seconds: 2));
          print("main isolate print");
        }
      }else {
        print(message);
      }
    });
  1. 编写新Isolate使用的print2方法,处理新Isolate内部的逻辑
static void print2(SendPort send){
 // 给新Isolate生成一个用来通信的ReceivePort
    ReceivePort rep2 = new ReceivePort();
  // 通过接收来的发送器,将自己持有的port发送器发送给之前的Isolate,实现两个iosLate的双向通讯
    send.send(rep2.sendPort);

// 为自己持有的rep2添加监听方法,处理之前Isolate发送来的信息
    rep2.listen((message) {
      if(message is String){
   // 当接收到String(刚才发送的"custom isolate print")时,调用耗时方法,测试异步操作是否成功
        for(int i = 0; i < 50; i++) {
          sleep(Duration(seconds: 2));
          print(message);
        }
      }
    });
  }
  1. 查看打印日志可以看到两个方法同时打印


    截屏2022-08-13 19.37.20.png

你可能感兴趣的:(flutter实现并发方法)