Google News
logo
Dart - Interview Questions
How do you implement concurrency in Dart?
In Dart, concurrency can be implemented using isolates. Isolates are independent units of execution that run concurrently and can communicate with each other through message passing. Each isolate has its own memory and runs in parallel with other isolates, allowing for true concurrent execution.

To implement concurrency in Dart, you can follow these steps:

1.  Spawn an Isolate : Use the `Isolate.spawn()` function to create a new isolate and start its execution. The `spawn()` function takes a callback function as an argument, which will be the entry point of the isolate.
   import 'dart:isolate';

   void isolateFunction(SendPort sendPort) {
     // Isolate execution logic
   }

   void main() {
     Isolate.spawn(isolateFunction, null);
   }​

   In the example above, the `isolateFunction` is the entry point of the spawned isolate. It receives a `SendPort` that can be used to communicate with the isolate.

2. Send and Receive Messages : Isolates communicate with each other by sending and receiving messages through `SendPort` and `ReceivePort` objects. The `SendPort` is used to send messages from one isolate to another, while the `ReceivePort` is used to receive messages.
  import 'dart:isolate';

   void isolateFunction(SendPort sendPort) {
     // Receiving messages
     ReceivePort receivePort = ReceivePort();
     sendPort.send(receivePort.sendPort);
     receivePort.listen((message) {
       print('Received message: $message');
     });

     // Sending messages
     sendPort.send('Hello from isolate!');
   }

   void main() async {
     ReceivePort mainReceivePort = ReceivePort();
     Isolate isolate = await Isolate.spawn(isolateFunction, mainReceivePort.sendPort);

     mainReceivePort.listen((message) {
       print('Received message in main: $message');
       isolate.kill();
     });
   }​
   In the example above, the main isolate creates a `ReceivePort` called `mainReceivePort` to receive messages from the spawned isolate. The spawned isolate sends its `ReceivePort` to the main isolate through the `SendPort`. The main isolate listens to messages on its `mainReceivePort` and prints them. The spawned isolate sends a message to the main isolate.

3. Handle Communication : To handle communication between isolates, you can use `SendPort` and `ReceivePort` objects. `SendPort` allows you to send messages to another isolate, while `ReceivePort` allows you to listen to incoming messages.
   import 'dart:isolate';

   void isolateFunction(SendPort sendPort) {
     sendPort.send('Hello from isolate!');
   }

   void main() async {
     ReceivePort receivePort = ReceivePort();
     Isolate isolate = await Isolate.spawn(isolateFunction, receivePort.sendPort);

     receivePort.listen((message) {
       print('Received message: $message');
       isolate.kill();
     });
   }​

   In this simplified example, the main isolate creates a `ReceivePort` to listen for messages. The spawned isolate sends a message to the main isolate through the `SendPort`. The main isolate listens for messages on its `receivePort` and prints the received message.

Concurrency in Dart using isolates allows you to achieve true parallel execution and enables you to take advantage of multi-core processors to perform computationally intensive tasks or handle concurrent I/O operations. By using message passing, isolates can communicate and share data in a controlled manner, ensuring safe concurrent execution.
Advertisement