What are isolates in Dart?
Dart was built for constructing single-page applications. However, considering the prevalence of multicore processors in most computers, including mobile platforms, developers commonly utilize shared-memory threads that execute concurrently. Nonetheless, concurrency based on a shared state can lead to intricate and error-prone code. Hence, all code runs within isolates instead of threads in Dart.
Isolates function differently compared to threads. Each isolate has its memory heap, ensuring that the state of any isolate remains inaccessible from other isolates. They interconnect by passing messages through channels. Therefore, requiring a method to serialize messages. In Dart, isolates communicate through message passing as clients and servers.
Learn to create an isolate in Dart
We must add the following statement to our code to use isolates:
import 'dart:isolate';
We use Dart’s .spawn() method to build an isolate. The syntax for the method is shown below:
Isolate <isolateName> = await Isolate.spawn( enter_parameter );
The <parameter> represents the port receiving the message back.
Learn to kill an isolate in Dart
We use the .kill() method in Dart to kill an isolate. The syntax for the method is shown below:
isolateName.kill( <enter_parameter> );
Using spawn() and kill() methods together in a single program
Let’s look at the code snippet below, where we create an isolate using the .spawn() method, receive messages, and terminate the isolate using the .kill() method.
// First we'll import the dart librariesimport 'dart:io';import 'dart:isolate';import 'dart:async';// Next, we create a new isolateIsolate isolate;void new_process() async{// We create a port for isolate to receive messages.ReceivePort portReceive= ReceivePort();// We start the isolateisolate = await Isolate.spawn(func, portReceive.sendPort);}void func(SendPort sendPort){int count = 0;// We print the output message every 1 sec.Timer.periodic(new Duration(seconds: 1), (Timer t) {// We increase the countercount++;//Next, we print the output messagestdout.writeln('Welcome to Educative $count');});}void end_process() {// We'll check the isolate with nullif (isolate != null) {stdout.writeln('Stop the Isolate');// Killing the isolateisolate.kill(priority: Isolate.immediate);// Setting the isolate to nullisolate = null;}}// Main Functionvoid main() async {stdout.writeln('Start the isolate');// We'll start the isolate with new_processawait new_process();// Assuming that the process takes 3 seconds to finishawait Future.delayed(const Duration(seconds: 3));// Here we call the end_processend_process();// Next we print the farewell messagestdout.writeln('Bye Bye!');// Finally, we exit the programexit(0);}
Explanation:
- Lines 2–4: We import Dart libraries into our code.
- Line 7: We declare an isolate.
- Line 13: We create a
ReceivePortfor the isolate to receive messages. - Line 16: We use the
.spawn()method to instantiate the isolate with the receiving port created earlier. - Lines 18–30: We print output to the console every second.
- Lines 32–43: We check if the isolate is not
null. We terminate the isolate using the.kill()method. - Lines 46–63: We define the main function, which waits asynchronously for three seconds to let the
new_process()print its messages to the console.
Free Resources