Table of contents
1.
Introduction
2.
How Dart Supports Concurrency
3.
Using Async, await and Future
4.
Frequently Asked Questions
4.1.
Why does the output of the isolate changes every time we run our code?
4.2.
What is the Future keyword?
4.3.
How multi-core CPU is suitable for concurrent programming?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Concurrency in Dart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be learning about the concurrency in dart. Concurrency is a mechanism that allows us to run multiple tasks simultaneously. In programming languages, we use this concept to run multiple code blocks concurrently. In this mechanism, we use the idea of multi-core processors to divide the work for different processors, and these processors execute the code concurrently. Generally, we execute multiple functions at the same time e.g. along with the main function, other functions are also executing and producing results.

If your CPU is a uniprocessor, then concurrency will not support because, in that case, when the CPU gets the new process, a simple scheduling algorithm will run, and the old process will get preempted.

How Dart Supports Concurrency

In dart, we can use the concurrency mechanism with the help of isolates. Isolates are the block of code running isolated on the processor. You may find isolates relatable to java's thread. If you don't know about the thread, read this article on the java thread. The main difference between thread and isolates is thread runs on the same shared memory while the isolates have their own individual memory, they do not have shared variables. The only way they can communicate is through messages which are sent at the time of creating the isolates. 

To use the isolate in your code, you need to import its library first.

import 'dart:isolate';


Syntax:

Isolate.spawn(New_Function, 'Message to be passed');

Isolate class in dart provides us with a static method called spawn. When we call the spawn method with the new function as the argument, the new function is invoked as a new isolate with only the message as a parameter.

The output of the code in a concurrent manner may differ from system to system we cannot simply decide the execution serial of the different functions in the concurrent background.

Example: In the example given below, we have two types of outputs for the same code executed two different times.

Code:

import 'dart:isolate';


void newFunction(var message) {
 // This is our isolate function.
 print('${message} inside Isolate');
}


void main() {
 // Calling the isolate function with new function and message.
 Isolate.spawn(newFunction, 'Hello fellas!');
 Isolate.spawn(newFunction, 'Coding');
 Isolate.spawn(newFunction, 'Ninjas');


 // Printing the message from the function.
 print('First message from main function');
 print('Second message from main function');
}


Output 1:

Hello fellas! inside Isolate
First message from main function
Second message from main function
Coding inside Isolate
Ninjas inside Isolate


Output 2:

First message from main function
Second message from main function
Hello fellas! inside Isolate
Ninjas inside Isolate
Coding inside Isolate

Using Async, await and Future

In the concurrent atmosphere, it is not easy to decide the sequence order of the execution of different functions. To solve this problem, dart has provided us a special functionality by using asyncawait and future keywords. Using these keywords, we can restrict the execution process in the concurrent execution.

The Async along with Await keywords are used to give a declarative means of defining and using asynchronous functions. When we want to declare a function as asynchronous, we use the async keyword, and we only use the await keyword on asynchronous functions. At the same time, the function which has await keyword at the time of its invocation is of type Future.

Example: The below example shows the same code as the above one but uses await, sync and future keywords. 

Code:

import 'dart:isolate';


Future newFunction(var message) async {
 //This function is of type future, this tells that its execution may takes some time.
 // This is our isolate function.
 print('${message} inside Isolate');
}


void main() async {
 //This function is of type async.


 // Calling the isolate function with new function and message.
 // await keywords make the execution wait for this function to get completed.
 await Isolate.spawn(newFunction, 'Hello fellas!');
 await Isolate.spawn(newFunction, 'Coding');
 await Isolate.spawn(newFunction, 'Ninjas');


 // Printing the message from the function.
 print('First message from main function');
 print('Second message from main function');
}


Output:

Hello fellas! inside Isolate
Coding inside Isolate
Ninjas inside Isolate
First message from main function
Second message from main function

 
Explanation: Every time we execute this code, it will give the same result, and even this will run accordingly on the other systems.

Frequently Asked Questions

Why does the output of the isolate changes every time we run our code?

If we do not use await, async and future keywords, then the output of the code will change every time we run the code because, in this case, we cannot decide the order of execution of the code. 

 

What is the Future keyword?

The result of an asynchronous operation is represented by a future, which can be in one of two states: uncompleted (still processing the result) or completed(function is done with the result). 

  

How multi-core CPU is suitable for concurrent programming?

When we divide the execution such that two or more functions of the same program execute parallelly, we need more than one processor for every parallel executing code. If Your CPU does not have multi-core, it will simply use a scheduling algorithm.

Conclusion

In this article, we've extensively discussed the Concurrency in dart and its implementation. We have discussed how to use its await, async and future keywords to control the order of execution.

We hope this blog has helped you enhance your knowledge regarding the Concurrency in dart. Do check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass