Dart Concurrency
Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance tasks. The concept of concurrency utilizes the unused skills of critical applications and machine hardware to make the program highly effective and increase its throughput. Dart supports concurrent programming with async-await, isolates, and classes such as Future and Stream.
How to achieve concurrency?
In Dart, Isolates help in achieving concurrency. We have discussed Dart isolates in a separate tutorial. Here we will understand the brief introduction of it. Dart isolate is a version of the Thread. But there is a key difference between the common implementation of "Thread" or "Isolates". The isolate works differently in comparison to Thread. The isolates are independent workers who do not share memory but interconnect by passing messages over channels. Since isolates complete their task by passing messages thus, it needs a way to serialize a message.
The connection between the isolates is made through a passing message as a client and server. It helps the system to take advantage of multicore microprocessors out of the box.
Dart offers a ' dart:isolate ' package to use isolate in our system. It provides a single-thread Dart code to capture solution and allows applications to make the most of available hardware.
Let's understand the following example -
Example -
import 'dart:isolate' ; // function containing the message to be printed void func( var message ) { print( ' We are executing this message from func( ) : ${ message } ' ) ; } void main( ) { Isolate.spawn( func,' This is an isolate program ! ' ) ; Isolate.spawn( func,' We are executing from main( ) function ! ' ) ; Isolate.spawn( func,' These messages are referring to func( ) function ! ' ) ; print( ' Executing from the main1( ) function ! ' ) ; print( ' Executing from the main2( ) function ! ' ) ; print( ' Executing from the main3( ) function ! ' ) ; }
Output 1 :
Executing from the main1( ) function ! Executing from the main2( ) function ! Executing from the main3( ) function ! We are executing this message from func( ) : These messages are referring to func( ) function ! We are executing this message from func( ) : This is an isolate program !
Output 2 :
Executing from the main1( ) function !
Executing from the main2( ) function !
Executing from the main3( ) function !
We are executing this message from func( ) : This is an isolate program !
We are executing this message from func( ) : We are executing from main( )
function !
Output 3 :
Executing from the main1( ) function ! Executing from the main2( ) function ! Executing from the main3( ) function ! We are executing this message from func( ) : We are executing from main( ) function !
We are executing this message from func( ) : This is an isolate program !
The concept of the code above is similar to dart isolate. If we use the above system many times, then the output will always be different.