Dart: What is Concurrency

Jinali GhoghariJinali Ghoghari
2 min read

Introduction

Concurrency in Dart refers to the ability of Dart programs to execute multiple tasks simultaneously, enabling efficient utilization of system resources and improving overall performance.

Dart offers several mechanisms to achieve concurrency, including asynchronous programming with Futures and Streams, as well as isolates.

Asynchronous Programming:

Asynchronous programming in Dart involves executing tasks concurrently, without waiting for each task to finish before starting the next one. This is useful for tasks like fetching data from a server, where waiting for a response would block the program's execution. Instead, you can initiate the operation and continue with other tasks. When the operation completes, you can handle the result asynchronously using callbacks or async and await syntax.

  • Futures:

    Think of futures as promises for values that will be available sometime in the future. In Dart, futures are used for asynchronous operations, such as fetching data from the internet or reading files. Instead of waiting for the operation to finish, your program continues executing other tasks. When the operation completes, the future "completes" with a value or an error, allowing you to handle the result.

  • Streams:

    Streams are like continuous flows of data. Imagine a stream of water flowing in a river – data keeps coming, and you can react to each piece of data as it arrives. In Dart, streams represent sequences of asynchronous events. They're commonly used for handling real-time data, such as user input or data from sensors. With streams, you can listen for events and respond to them as they occur, enabling reactive programming patterns.

In summary, in Dart concurrency:

  • Asynchronous programming enables you to execute tasks concurrently without blocking the program's execution.

  • Futures represent asynchronous operations and allow you to work with values that will be available in the future.

  • Streams provide a way to handle continuous flows of asynchronous events, allowing you to react to data as it arrives in real-time.

Isolates:

as the name suggests, are isolated units of running code. The only way to send data between them is by passing messages, like the way you pass messages between the client and the server. An isolate helps the program to take advantage of multicore microprocessors out of the box.

0
Subscribe to my newsletter

Read articles from Jinali Ghoghari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jinali Ghoghari
Jinali Ghoghari