Design a site like this with WordPress.com
Get started

Flutter’s Concurrency: Isolate & Async-Await in Dart

Isolate: An isolate is a specific chunk of memory generated by Dart to handle specific tasks . One isolate can be tasked to handle a single or multiple tasks asynchronously . If there is a need to perform high-computation or intensive task, that is where isolates enable Dart to achieve concurrency and multithreading . An isolate’s concept may sound similar to multithreads in other languages .

Isolate Advantages:
1. Achieving Concurrency
2. Each isolate’s state is secured from other isolates’ interference
3. No Mutex or Thread Lock due to separate memory heap for each isolate
4. Performing multiple independent tasks at a time
5. Offloading heavy computation to worker isolates (as background workers)

Async-Await: ‘async’ and ‘await’ are two keywords reserved in Dart language and used with Flutter . When the main isolate does multiple works at a time to get things done faster, this characteristic is called to be asynchronous . ‘await’ keyword is used before the function that runs to bring the result .

The Dart program below implements isolates to calculate separately and finally displays each Isolate’s ID:

import ‘dart:isolate’;
import ‘dart:developer’;

// iso() takes a number and performs addition using specific different Isolates void iso(var n){
print(‘${n+n} and isolate ID is: ‘);
// displaying Isolate ID to ensure the difference
print(Service.getIsolateID(Isolate.current));
}    

void main() async {  
// Spawning (Producing) three different Isolates
await Isolate.spawn(iso,9);   
await Isolate.spawn(iso,200001);   
await Isolate.spawn(iso,1);
// The kill() function kills an Isolate
// Isolate.kill(priority: Isolate.immediate);
}

Calculation using Isolates


Communication Between Or Among Isolates By Message Passing

One isolate can not interrupt another isolate’s state, data or memory. When they need to communicate with each other, an isolate passes asynchronous messages through a SendPort and the intended receiver isolate receives those messages through its correspnding ReceivePort .

The Dart program below highlights message passing between isolates:

import ‘dart:io’;
import ‘dart:async’;
import ‘dart:isolate’;

Isolate? isolate;

start() async {
// The port for an isolate to receive messages
ReceivePort receivePort= ReceivePort();
isolate = await Isolate.spawn(runTimer, receivePort.sendPort);
receivePort.listen((data) {
stdout.write(‘Receiving: ‘ + data + ‘, ‘);
});
}

void runTimer(SendPort sendPort) {
int counter = 0;
Timer.periodic(new Duration(seconds: 1), (Timer t) {
counter++;

String msg = ‘notification ‘ + counter.toString();  
stdout.write(‘Sending: ‘ + msg + ‘ -> ‘);  
sendPort.send(msg);
});
}

stop() {
if(isolate != null) {
stdout.writeln(‘Stopping the isolate.’);
//  isolate.kill(priority: Isolate.immediate);
isolate = null;        
}  
}

void main() async {
stdout.writeln(‘Starting an isolate.’);
await start();
stdout.writeln(‘Press the Enter key to stop.’);
await stdin.first;
stop();
stdout.writeln(‘Ending the program.’);
exit(0);
}

Isolate Message Passing
Advertisement

Published by Farial Mahmod Tishan

Life-long learner. Developing Flutter apps on Parrot Linux OS .

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: