Dart编程 - 并发(Concurrency)
优质
小牛编辑
151浏览
2023-12-01
Concurrency是同时执行多个指令序列。 它涉及同时执行多个任务。
Dart使用Isolates作为并行工作的工具。 dart:isolate包是Dart的解决方案,用于获取单线程Dart代码并允许应用程序更多地使用可用的硬件。
Isolates ,顾名思义,是运行代码的独立单元。 在它们之间发送数据的唯一方法是传递消息,就像在客户端和服务器之间传递消息的方式一样。 isolate可帮助程序充分利用多核微处理器的优势。
例子 (Example)
让我们举个例子来更好地理解这个概念。
import 'dart:isolate';
void foo(var message){
print('execution from foo ... the message is :${message}');
}
void main(){
Isolate.spawn(foo,'Hello!!');
Isolate.spawn(foo,'Greetings!!');
Isolate.spawn(foo,'Welcome!!');
print('execution from main1');
print('execution from main2');
print('execution from main3');
}
这里, Isolate类的spawn方法有助于与我们的其余代码并行运行函数foo 。 spawn函数有两个参数 -
- 产生的功能,和
- 将传递给衍生函数的对象。
如果没有对象传递给生成的函数,则可以传递NULL值。
这两个函数(foo and main)可能不一定每次都以相同的顺序运行。 无法保证foo何时执行以及何时执行main() 。 每次运行时输出都不同。
输出1
execution from main1
execution from main2
execution from main3
execution from foo ... the message is :Hello!!
输出2
execution from main1
execution from main2
execution from main3
execution from foo ... the message is :Welcome!!
execution from foo ... the message is :Hello!!
execution from foo ... the message is :Greetings!!
从输出中,我们可以得出结论,Dart代码可以从运行代码中生成新的isolate ,就像Java或C#代码可以启动新线程一样。
Isolates与线程的不同之处在于isolate有自己的内存。 无法在isolates之间共享变量 - isolates区之间进行通信的唯一方法是通过消息传递。
Note - 对于不同的硬件和操作系统配置,上述输出将有所不同。
隔离v/s Future
异步执行复杂的计算工作对于确保应用程序的响应性非常重要。 Dart Future是一种在完成后检索异步任务的值的机制,而Dart Isolates是一种抽象并行性并在实际的高级基础上实现它的工具。