Will Dart execute isolates in parallel in a multi-core environment?

后端 未结 3 465
终归单人心
终归单人心 2021-02-01 06:18

Question

Will isolates in Dart run in parallel utilizing all available cores on a multiple core environment, or will it multiplex on a single core?

Background

3条回答
  •  长发绾君心
    2021-02-01 06:58

    Here is updated code for Dart 1.0.

    import 'dart:isolate';
    
    main() {
      int counter = 0;
    
      ReceivePort receivePort = new ReceivePort();
    
      receivePort.listen((msg) {
        if (msg is SendPort) {
          msg.send(counter++);
        } else {
          print(msg);
        }
      });
    
      for (var i = 0; i < 5; i++) {
        Isolate.spawn(runInIsolate, receivePort.sendPort);
      }
    }
    
    runInIsolate(SendPort sendPort) {
      ReceivePort receivePort = new ReceivePort();
      sendPort.send(receivePort.sendPort);
    
      receivePort.listen((msg) {
        var k = 0;
        var max = (5 - msg) * 100000000; 
        for (var i = 0; i < max; ++i) {
            i = ++i - 1;
            k = i;
        }
        sendPort.send("I received: $msg and calculated $k");
      });
    }
    

提交回复
热议问题