本文介绍了扑孤立与未来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对隔离与未来的想法可能是错误的。请帮助我清除它。这是我对这两个主题的理解。

I might have the wrong idea of Isolate and Future. Please help me to clear it up. Here is my understanding of both subjects.

隔离:运行代码,并且每个事件都可以在嵌套的微任务队列中运行较小的任务。

Isolate: Isolates run code in its own event loop, and each event may run smaller tasks in a nested microtask queue.

未来:期货用于表示将来某个时候可用的潜在价值或错误。

Future: A Future is used to represent a potential value, or error, that will be available at some time in the future.

我的困惑是:


  1. 医生说隔离有自己的循环?我觉得拥有自己的事件队列对我来说更有意义,我错了吗?

  1. The doc says Isolate has it own loop? I feel like having its own event queue makes more sense to me, am I wrong?

将来是否可以在主要Isolate上异步运行?我假设将来的任务实际上已放置在事件队列的末尾,因此将来是否可以通过循环执行。如果我错了,请纠正我。

Is future running asynchronously on the main Isolate? I'm assuming future task actually got placed at the end of event queue so if it will be execute by loop in the future. Correct me if I'm wrong.

为什么在将来有机会使用隔离?我看到一些使用Isolate代替Future进行繁重任务的示例。但为什么?仅当将来在主隔离队列上异步执行时,这对我才有意义。

Why use Isolate when there is future? I saw some examples using Isolate for some heavy task instead of Future. But why? It only makes sense to me when future execute asynchronously on the main isolate queue.


推荐答案

A 未来是一个句柄,可让您在异步执行完成时得到通知。
异步执行使用事件队列,并且代码在同一线程中同时执行。

A Future is a handle that allows you to get notified when async execution is completed.Async execution uses the event queue and code is executed concurrently within the same thread.

Dart代码默认是在根隔离中执行的。

Dart code is by default executed in the root isolate.

您可以启动通常在另一个线程上运行的其他隔离。
隔离源可以从启动根隔离源的同一Dart代码加载(入口点与 main() )或使用其他Dart代码(从某些Dart文件或URL )。

You can start up additional isolates that usually run on another thread.An isolate can be either loaded from the same Dart code the root isolate was started with (with a different entry-point than main() https://api.dartlang.org/stable/2.0.0/dart-isolate/Isolate/spawn.html) or with different Dart code (loaded from some Dart file or URL https://api.dartlang.org/stable/2.0.0/dart-isolate/Isolate/spawnUri.html).

隔离没有共享任何状态,只能使用消息传递(SendPort / ReceivePort)进行通信。每个隔离都有自己的事件队列。

Isolates don't share any state and can only communicate using message passing (SendPort/ReceivePort). Each isolate has its own event queue.

这篇关于扑孤立与未来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:02