Dart Isolates

In this tutorial you will learn about the Dart Isolates and its application with practical example.

Dart Isolates

Dart supports asynchronous programming which let your program run without getting blocked. Dart uses isolates in order to achieve concurrency. We can think of an Isolate corresponds to the Dart version of the Thread. However, there is a major difference with the usual implementation of “Threads” and “Isolates”. The Isolates are independent workers that do not share memory, but instead communicate by passing messages over channels, isolates are similar to actors in Erlang or web workers in JavaScript. Since isolates are communicating via passing messages thus it needs a way to serialize a message.

Create and Start an Isolate

An Isolate can be created using the spawn() method. The spawn method must be provided with an ‘entry point’ method with a single parameter. This parameter represents a port which isolate will use to send back notification messages.

Example:-

Here, we have a start() method which creates a port and spawns an isolate. The start() method is marked as async so that we can await the response from the spawning of the isolate and to store a reference to the new isolate (Which is important when we want to kill a running isolate). The spawn() method provided with two parameters that is a callback function to execute runTimer(), and a port sendPort which the callback function will use to send messages back to the caller. The, start() method begin listening the receivePort for messages from the isolate. As this will receive a message, it will send it to the console output. The runTimer() method starts a Timer that fires every second in order to update a counter and send out a notification message via the port which it received when the isolate was spawned.

Recommended:-  Dart Symbol

Stop an Isolate

The kill() method is used to stop a running isolate.

Example:-

Here, we have created a stop() method that will kill the running isolate and sets its reference to null. The priority of Isolate.immediate will terminate the isolate at earliest.

Complete Program In Action

Example:-

Output-

dart_isolates_example

In this tutorial we have learn about the Dart Isolates and its application with practical example. I hope you will like this tutorial.