- Dart Programming - HTML DOM
- Dart Programming - Unit Testing
- Dart Programming - Concurrency
- Dart Programming - Async
- Dart Programming - Libraries
- Dart Programming - Typedef
- Dart Programming - Debugging
- Dart Programming - Exceptions
- Dart Programming - Packages
- Dart Programming - Generics
- Dart Programming - Collection
- Dart Programming - Object
- Dart Programming - Classes
- Dart Programming - Interfaces
- Dart Programming - Functions
- Dart Programming - Enumeration
- Dart Programming - Runes
- Dart Programming - Symbol
- Dart Programming - Map
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Boolean
- Dart Programming - String
- Dart Programming - Numbers
- Dart Programming - Decision Making
- Dart Programming - Loops
- Dart Programming - Operators
- Dart Programming - Variables
- Dart Programming - Data Types
- Dart Programming - Syntax
- Dart Programming - Environment
- Dart Programming - Overview
- Dart Programming - Home
Dart Programming Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Dart Programming - Concurrency
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 apppcation 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, pke the way you pass messages between the cpent 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 faciptates 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.
Output 1
execution from main1 execution from main2 execution from main3 execution from foo ... the message is :Hello!!
Output 2
execution from main1 execution from main2 execution from main3 execution from foo ... the message is :Welcome!! execution from foo ... the message is :Hello!! execution from foo ... the message is :Greetings!!
From the outputs, we can conclude that the Dart code can spawn a new isolate from running code pke the way Java or C# code can start a new thread.
Isolates differ from threads in that an isolate has its own memory. There’s no way to share a variable between isolates—the only way to communicate between isolates is via message passing.
Note − The above output will be different for different hardware and operating system configurations.
Isolate v/s Future
Doing complex computational work asynchronously is important to ensure responsiveness of apppcations. Dart Future is a mechanism for retrieving the value of an asynchronous task after it has completed, while Dart Isolates are a tool for abstracting parallepsm and implementing it on a practical high-level basis.
Advertisements