×

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.


Related Topics

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.