什么是并发?2024 年 8 月 29 日 | 阅读 2 分钟 Dart 并发允许我们同时运行多个程序或程序的多个部分。它同时执行多个指令。Dart 提供 Isolates 作为并行工作的工具。通过利用基本操作系统和机器硬件的未使用的功能,并发性使程序高效和吞吐量。 如何实现并发?在 Dart 中,我们可以通过使用 Isolates 来实现并发。我们在之前的教程中讨论过 Dart isolates。在这里我们将了解它的简要介绍。Dart isolate 是线程的一个版本。但是 “Thread” 或 “Isolates” 的常见实现之间存在关键差异。与线程相比,isolate 的工作方式不同。Isolate 是独立的 workers,它们不共享内存,而是通过在通道上传递消息来互连。由于 isolate 通过传递消息来完成其任务,因此它需要一种序列化消息的方式。 Isolate 之间的通信通过消息传递作为客户端和服务器来完成。它有助于程序开箱即用地利用多核微处理器。 Dart 提供了 dart:isolate 包,以便在我们的程序中应用 isolate。它提供了解决单线程 Dart 代码的方案,并允许应用程序更好地利用可用的硬件。 让我们理解以下示例 - 示例 -输出 execution from main1 execution from main2 execution from main3 execution from sayhii ... the message is :Whats up!! execution from sayhii ... the message is :Hello!! 输出 2 execution from main1 execution from main2 execution from main3 execution from sayhii ... the message is :Hello!! execution from sayhii ... the message is :Welcome!! 上述代码的概念类似于 Dart isolates。如果我们多次运行上述程序,则每次输出都会有所不同。 下一个主题Dart 单元测试 |
我们请求您订阅我们的新闻通讯以获取最新更新。