首页 > 其他分享 >dart isolate

dart isolate

时间:2023-01-07 17:45:50浏览次数:31  
标签:map sendPort isolate send dart ReceivePort message

isolate的理解

isolate原型

external static Future<Isolate> spawn<T>(
    void entryPoint(T message), T message,
    {bool paused = false,
    bool errorsAreFatal = true,
    SendPort? onExit,
    SendPort? one rror,
    @Since("2.3") String? debugName});

isolate功能和名字基本一直,能够调用另外的CPU内核
进程与线程是操作系统的概念,进程间的内存空间是隔离,是相互看不见,摸不着,访问不了的。而一个进程内的多个线程,共用同一个地址空间,也即多线程之间内存是完全共享的
dart的isolate就是isolate,是自己语言本身的一个特点
如下是isolate使用其下面的spawn来创建一个新的isoalte,isolate有着独立的堆栈(可以这样理解:如果使用isolate外面的变量isolate会创建一个新的变量,并且将值进行赋值),我们一般只能是从isolate里面向外传出消息,isolate规定如果message传递的是port的话,只能是sendport,也就是只能向外发发送消息,那怎么向里面发送消息呢,也就在里面发送一个sendPort给外面就行了经过测试,在里面向外发送receivePort会报错

void main() async {
  //1.创建管道
  ReceivePort receivePort1 = ReceivePort();
  //2.创建Isolate
  Isolate.spawn<SendPort>((SendPort send) async {
    ReceivePort receivePort2 = ReceivePort();
    receivePort2.listen((message) {
      print("isolate->${message}");
    });
    send.send({"sendPort": receivePort2.sendPort});
  }, receivePort1.sendPort);
  //3.监听管道
  receivePort1.listen((message) {
    Map<String, dynamic> map;
    map = message as Map<String, dynamic>;
    if(map["sendPort"] != null){
      print("receive sendPort");
      int a = 0;
      SendPort sendPort = map["sendPort"];
      while(true){
        sendPort.send(a++);
        sleep(const Duration(seconds: 1));
      }
    }else{
      print(map["data"]);
    }
  });
}

什么是并行

The underlying idea in parallel computing is that the computational problem can be split into smaller subtasks. Multiple subtasks can then be executed simultaneously by multiple processing units. In modern CPUs, the single execution unit is typically a CPU core.

因此isolate是实现并行的一种体现

类似于isolate的还有java的thread,go的协程

标签:map,sendPort,isolate,send,dart,ReceivePort,message
From: https://www.cnblogs.com/sqmw/p/17033093.html

相关文章

  • dart HttpSocket&HttpClient
    HttpSocketvarserver=awaitHttpServer.bind(InternetAddress.loopbackIPv4,4040,);print('Listeningonlocalhost:${server.port}');awaitfor(HttpReq......
  • Dart中不得不会的mixins
    文章目录​​一、mixins是什么?​​​​二、使用场景​​​​三、注意​​​​四、代码案例​​一、mixins是什么?在面向对象的编程语言中,mixin(或mix-in)是一个类,其中包含供其......
  • [ensp自学]2.端口隔离port-isolate
    端口隔离是为了在同一vlan下阻止某些端口之间不可以互相访问。加入端口隔离相同组的内部成员之间不可以互通。PC1和PC2不能互通,都可以访问PC3,PC3可以访问PC2不可以访问PC1<......
  • 【LeetCode2180】[Go/C++/C#/Ruby/Swift/Kotlin/Rust/PHP/TS/Racket/Dart/Java/Elixir
    [toc]题解地址https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/2047123/by-yhm138_-w8co/lc2180代码//点击指定元素document.querySel......
  • dart Socket and ServerSocket
    ServerSocketvoidmain()async{ServerSocketserverSocket=awaitServerSocket.bind("localhost",8888);serverSocket.listen((Socketclient){client......
  • E - Don't Isolate Elements(DP,状态设计)
    E-Don'tIsolateElements题意​ 给出一个01矩阵,长为\(n\),宽为\(m\)。现在你可以进行一个操作:任选一行,将其该行上的0变1,1变0。请问最少需要多少次操作,可以使得整......
  • go-zero一键生成 Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript 代码,并可
    go-zero是一个集成了各种工程实践的web和rpc框架。通过弹性设计保障了大并发服务端的稳定性,并经受了充分的实战检验(好未来-晓黑板)。go-zero包含极简的API定义......
  • Flutter如何调试应用【Dart Observatory 】以及调试模式断言
    Flutter如何调试应用我们上面写了Flutter测试应用,这远远不够,这篇,我们来写一下Flutter如何调试应用:voidsomeFunction(doubleoffset){debugger(when:offset>30.0);......
  • dart 语法特性
    dart和JS长得很像数据类型(变量)dart语言所有的类型都是对象强类型语言,但是也可以使用var不显示地申明变量,此时在我们赋值后会进行类型判定,如是不需要类型判断的话,就需......
  • abc 283 E Don't Isolate Elements
    abc283EDon'tIsolateElements题意:给出一个\(h*w\)的01矩阵,对于每一行,可以进行翻转操作。如果\(a_{i,j}\)的上下左右没有一个和它数值一样的话,这个点就被称......