Concurrency is the execution of several instruction sequences at the same time. It involves performing more than one task simultaneously.
Dart uses Isolates as a tool for doing works in parallel. The dart:isolate package is Dart’s solution to taking single-threaded Dart code and allowing the application to make greater use of the hard-ware available.
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.
Example
Let’s take an example to understand this concept better.
import ‘dart:isolate’;
void foo(var message){
print(‘execution from foo … the message is :${message}’);
}
void main(){
Isolate.spawn(foo,’Hello!!’);
Isolate.spawn(foo,’Greetings!!’);
Isolate.spawn(foo,’Welcome!!’);
print(‘execution from main1’);
print(‘execution from main2’);
print(‘execution from main3’);
}
Here, the spawn method of the Isolate class facilitates running a function, foo, in parallel with the rest of our code. The spawn function takes two parameters −
the function to be spawned, and
an object that will be passed to the spawned function.
In case there is no object to pass to the spawned function, it can be passed a NULL value.
The two functions (foo and main) might not necessarily run in the same order each time. There is no guarantee as to when foo will be executing and when main() will be executing. The output will be different each time you run.