Isolate字面意思是隔离,即每个Isolate是独立的,隔离的,内存不共享的。
官方文档或注释的一部分:
All Dart code runs in an isolate, and code can access classes and values only from the same isolate.
所有的 Dart 代码运行在一个 Isolate 里,代码只能访问同一个 Isolate 里的类和值。
Different isolates can communicate by sending values through ports.
不同的 isolate 能够通过 port 发送值进行交流。
An isolate is what all Dart code runs in. It’s like a little space on the machine with its own, private chunk of memory and a single thread running an event loop.
所有 Dart 代码运行在一个 Isolate 里,它像机器上的一个小空间,有自己的私有内存块和运行事件循环的单个线程。
① Isolate.spawn
② compute()
Isolate是如何实现内存隔离的参考文章:[ Dart 中的
这里的内存指的就是堆内存,每个Isolate的堆内存是随着Isolate的创建而初始化。
Isolate之间的通信DartVM支持的消息数据类型为:
① 原始数类型,如null、bool、double、int、String等
② SendPort实例,比如ReceivePort().sendPort
③ 包含①和②的list和map,也可以嵌套
④ 在DartVM中,处于同一进程的2个Isolate,也可以发送自定义的Class实例对象,但dart2js编译器不可以。
Isolate示例① 创建Isolate
② 准备获取发送过来的数据
③ 将2边sendPort进行绑定
④ 创建监听,监听那边发过来的数据和SendPort
⑤ 开始往那边发送数据和SendPort
⑥ 等待那边处理数据
⑦ 监听到了那边发过来的数据和SendPort
⑧ 用拿到的数据进行大量的计算
⑨ 开始大量计算
⑩ 将计算完的数据发到那边
import 'dart:async';import 'dart:isolate';import 'package:flutter/material.dart';void main() { runApp(MyApp());}class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: HomePage(), ); }}class HomePage extends StatelessWidget { const HomePage({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Column( children: [ MaterialButton( color: Colors.red, onPressed: test, child: Text('测试'), ), ], ), ); } void test() { asyncFibonacci(3).then((value) { print('执行:11'); // ----> 11. 拿到返回值 print(value); }); /* flutter: 执行:1 flutter: 执行:2 flutter: 执行:3 flutter: 执行:4 flutter: 执行:5 flutter: 执行:6 flutter: 执行:7 flutter: 执行:8 flutter: 执行:9 flutter: 执行:9 flutter: 执行:9 flutter: 执行:9 flutter: 执行:9 flutter: 执行:10 flutter: 执行:11 flutter: 2 */ } /// 创建Isolate来计算斐波那契数列第n个数 /// n从0开始 Future<dynamic> asyncFibonacci(int n) async { /// 创建Isolate /// 创建一个ReceivePort /// Isolate所需参数,必须要有SendPort,SendPort需要ReceivePort创建 final receivePort = ReceivePort(); /// 第一个参数entryPoint:必须是一个顶层方法或静态方法 /// 第二个参数message:通常初始化message包含一个sendPort print('执行:1'); // ----> 1. 创建Isolate await Isolate.spawn(_isolateTopLevelFunction, receivePort.sendPort); /// 获取sendPort来发送数据 print('执行:2'); // ----> 2. 准备获取发送过来的数据 final sendPort = await receivePort.first as SendPort; /// 接收消息的receivePort final answerReceivePort = ReceivePort(); /// 发送数据 print('执行:5'); // ----> 5. 开始往那边发送数据和SendPort sendPort.send([Calculate(count: n), answerReceivePort.sendPort]); /// 获取数据并返回 print('执行:6'); // ----> 6. 等待那边处理数据 return answerReceivePort.first; }}/// Isolate的顶级方法void _isolateTopLevelFunction(SendPort sendPort) { final receivePort = ReceivePort(); /// 绑定 print('执行:3'); // ----> 3. 将2边sendPort进行绑定 sendPort.send(receivePort.sendPort); /// 监听 print('执行:4'); // ----> 4. 创建监听,监听那边发过来的数据和SendPort receivePort.listen((message) { print('执行:7'); // ----> 7. 监听到了那边发过来的数据和SendPort /// 获取数据并解析 final data = message[0] as Calculate; final send = message[1] as SendPort; /// 返回结果 print('执行:8'); // ----> 8. 用拿到的数据进行大量的计算 int num = syncFibonacci(data.count); print('执行:10'); // ----> 10. 将计算完的数据发到那边 send.send(num); });}/// 同步的斐波那契计算顶级方法int syncFibonacci(int n) { print('执行:9'); // ----> 9. 开始大量计算 return n < 2 ? n : syncFibonacci(n - 2) + syncFibonacci(n - 1);}class Calculate { int count; Calculate({required this.count});}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。