create account

A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight by tensor

View this thread on: hive.blogpeakd.comecency.com
· @tensor · (edited)
$46.75
A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight
![dart-bird.jpg](https://cdn.steemitimages.com/DQmVf52JbNULs9kr7M9BTaZfgoEtAUdHc9VpVvGpuu4mg24/dart-bird.jpg)

#### Repository
https://github.com/dart-lang/sdk

#### What Will I Learn?

- You will learn about Concurrency in Dart
- You will learn about Parallel programming in Dart
- You will learn how to use Futures to perform Asynchronous Evaluations in Dart
- You will learn about Completers
- You will learn how to Spawn an Isolate 
- You will learn how to use Ports to Send and Receive Messages

#### Requirements
##### System Requirements: 
- [IDEA intellij](https://www.jetbrains.com/idea/) or [Visual Studio Code](https://code.visualstudio.com/) with the Dart [Plugins](https://dartcode.org/)
- The [Dart SDK](https://www.dartlang.org/) 

##### OS Support for Dart:
- Windows
- macOS
- Linux 

#### Required Knowledge
- The Dart SDK
- A Dart supported text editor ([Dart Pad can be used](https://dartpad.dartlang.org))
- A little time to sit and watch a video and some patience to learn the language

#### Resources for Dart:
- Dart Website: https://www.dartlang.org/
- Dart Official Documentation: https://www.dartlang.org/guides/language/language-tour
- Dart GitHub repository: https://github.com/dart-lang/sdk
- Awesome Dart GitHub Repository: https://github.com/yissachar/awesome-dart
- Pub website: https://pub.dartlang.org

#### Sources:
Dart Logo (Google): https://www.dartlang.org/

#### Difficulty

- Beginner


#### Description

In this Dart Video Tutorial, we take a look at how we can preform Concurrency and Parallel programming in Dart.  We take a look at Futures and Completers.  We also look at Isolates and their Ports and Messages.  We talk about why Asynchronous Evaluation is necessary and powerful and we talk about  the difference between Parallel programming and Asynchronous programming. 

#### Using Futures and Completers to Preform Asynchronous Tasks

Often when dealing with systems that are outside of the control of your current program; you have to deal with task that may take unspecified amounts of time to complete.  If you run these tasks in a synchronous manner, they block the code that follows the execution of the task.  This becomes a problem especially when dealing with computations that might fail or might take very long periods of time.  To overcome this problem, Dart exposes a Future API that uses Completer Objects to define when the task has completed.  These Futures allow Dart to specify Events which are pushed into an external Queue.  When the Completer object comes back with a completion, the Future then wraps the result of this completion and serves it where it is needed. 

<center>
![thread-dart.png](https://cdn.steemitimages.com/DQmdWvcf1CqztPT1gZGkRcaTdV6KY5UNfb9oXVQVzcQaejJ/thread-dart.png)
</center>

The above Graph highlights Dart's main execution loop.  Dart is a single threaded programming language and when a program starts it checks two main queues.  It first checks a microtask queue to see if there are any scheduled microtasks.  If there are, then it will execute those microtasks and loop back to re-check the queue.  If there are none, then the execution will move to the Event queue.  This Event queue is where Futures get logged and queued for execution. Dart executes the Event and then moves back to check to see if there are any Microtasks and Events in the Queues before finishing its execution once the Queues are empty.


#### Creating Parallel code with Dart using Isolates

Dart is inherently a single threaded programming language.  The main execution loop allows the program to work with asynchronous code but another thread is needed to create parallel code.  This is where the concept of the Isolate comes into play. Each isolate is another instance of the same execution loop that was highlighted above.  These instances do not share memory between one another and can only communicate using messages.  These messages are passed through unidirectional Ports which means that an Isolate can only either send data to another Isolate or receive data from another Isolate.  

<center>
![isolate-graph.png](https://cdn.steemitimages.com/DQmT74KheUfneyVASVx2KN1ZNKNxAqje7wMhd9mpGjGgyNx/isolate-graph.png)
</center>

Above is a graph that showcases the general concept behind isolates.  Isolate One which is the main isolate spawns isolate two in this case and it does so at the very beginning of execution.  Both isolates have identical execution lifecycles with different microtasks and events depending on the tasks that have been assigned to them by the developers.  Both have their own Event and Microtask Queues which allows these isolates to deal with Asynchronous code.  Isolate Two sends messages to Isolate One and it does this through a Port.  Inside of Isolate Two a Send Port Object and inside of Isolate One is a Receive Port Object.  These two objects facilitate the message passing between these two Isolates.  

The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-7


#### Video Tutorial
<iframe width="560" height="315" src="https://www.youtube.com/embed/DC_LxSvOMMI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>



#### Curriculum 
- [A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---generics-exceptions-factory-constructors-and-enumerated-types----part-seven)
- [A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six)
- [A Beginners Guide to Dart - Inheritance, Abstract Classes, Interfaces, Mixins and Casting - Part Five](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---inheritance-abstract-classes-interfaces-mixins-and-casting---part-five)
- [A Beginners Guide to Dart - Methods, Final, Static, and Class Inheritance - Part Four](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---methods-final-static-and-class-inheritance---part-four)
- [A Beginners Guide to Dart - Intro to Classes and Objects - Part Three](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---intro-to-classes-and-objects---part-three)
- [A Beginners Guide to Dart - Control Flow and Low Level Compilation - Part Two](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---control-flow-and-low-level-compilation---part-two)
- [A Beginners Guide to Dart - Types, Functions, Variables and Objects - Part One](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---types-functions-variables-and-objects---part-one)

#### Projects
- [Dart Project - Building a Client Side Tetris Game - Part One](https://steemit.com/utopian-io/@tensor/dart-project---building-a-client-side-tetris-game---part-one)
- [Dart Project - Building a Client Side Tetris Game - Part Two](https://steemit.com/utopian-io/@tensor/dart-project---building-a-client-side-tetris-game---part-two)
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 254 others
properties (23)
authortensor
permlinka-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight-
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","steemstem","technology","programming"],"app":"steemit/0.1","image":["https://cdn.steemitimages.com/DQmVf52JbNULs9kr7M9BTaZfgoEtAUdHc9VpVvGpuu4mg24/dart-bird.jpg","https://cdn.steemitimages.com/DQmdWvcf1CqztPT1gZGkRcaTdV6KY5UNfb9oXVQVzcQaejJ/thread-dart.png","https://cdn.steemitimages.com/DQmT74KheUfneyVASVx2KN1ZNKNxAqje7wMhd9mpGjGgyNx/isolate-graph.png","https://img.youtube.com/vi/DC_LxSvOMMI/0.jpg"],"links":["https://github.com/dart-lang/sdk","https://www.jetbrains.com/idea/","https://code.visualstudio.com/","https://dartcode.org/","https://www.dartlang.org/","https://dartpad.dartlang.org","https://www.dartlang.org/guides/language/language-tour","https://github.com/yissachar/awesome-dart","https://pub.dartlang.org","https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-7","https://www.youtube.com/embed/DC_LxSvOMMI","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---generics-exceptions-factory-constructors-and-enumerated-types----part-seven","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---inheritance-abstract-classes-interfaces-mixins-and-casting---part-five","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---methods-final-static-and-class-inheritance---part-four","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---intro-to-classes-and-objects---part-three","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---control-flow-and-low-level-compilation---part-two","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---types-functions-variables-and-objects---part-one","https://steemit.com/utopian-io/@tensor/dart-project---building-a-client-side-tetris-game---part-one","https://steemit.com/utopian-io/@tensor/dart-project---building-a-client-side-tetris-game---part-two"],"format":"markdown"}
created2019-03-22 00:44:24
last_update2019-03-22 00:46:12
depth0
children8
last_payout2019-03-29 00:44:24
cashout_time1969-12-31 23:59:59
total_payout_value35.200 HBD
curator_payout_value11.549 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,182
author_reputation87,856,203,149,624
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id81,708,848
net_rshares76,276,967,097,502
author_curate_reward""
vote details (318)
@rosatravels ·
$9.28
Hi @tensor,

Thank you for another great turtorial  on Dart.  

Your flow chart graphs are very effective in  showing how the programming works.  This is a great help to newbies who are starting to learn.


Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/9/1-1-1-1-1-1-1-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , ,
properties (23)
authorrosatravels
permlinkre-tensor-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190322t082744304z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["tensor"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/1-1-1-1-1-1-1-1-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-03-22 08:27:48
last_update2019-03-22 08:27:48
depth1
children2
last_payout2019-03-29 08:27:48
cashout_time1969-12-31 23:59:59
total_payout_value7.046 HBD
curator_payout_value2.233 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length659
author_reputation422,827,447,688,168
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,723,859
net_rshares14,253,351,590,287
author_curate_reward""
vote details (12)
@tensor ·
Thanks @rosatravels; I figured that would be a better way to show off the concepts compared to just showing code.
properties (22)
authortensor
permlinkre-rosatravels-re-tensor-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190322t222101140z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["rosatravels"],"app":"steemit/0.1"}
created2019-03-22 22:21:00
last_update2019-03-22 22:21:00
depth2
children0
last_payout2019-03-29 22:21:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length113
author_reputation87,856,203,149,624
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,762,341
net_rshares0
@utopian-io ·
Thank you for your review, @rosatravels! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-tensor-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190322t082744304z-20190324t092859z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-24 09:29:00
last_update2019-03-24 09:29:00
depth2
children0
last_payout2019-03-31 09:29:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length63
author_reputation152,955,367,999,756
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,842,835
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @tensor!

You just got a **6.45%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinka-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight----vote-steemplus
categoryutopian-io
json_metadata{}
created2019-03-23 00:06:12
last_update2019-03-23 00:06:12
depth1
children0
last_payout2019-03-30 00:06:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length433
author_reputation247,952,188,232,400
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,766,310
net_rshares0
@steem-ua ·
#### Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190322t091640z
categoryutopian-io
json_metadata"{"app": "beem/0.20.18"}"
created2019-03-22 09:16:42
last_update2019-03-22 09:16:42
depth1
children0
last_payout2019-03-29 09:16:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length285
author_reputation23,214,230,978,060
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,725,362
net_rshares0
@steemitboard ·
Congratulations @tensor! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@tensor/votes.png?201903250706</td><td>You made more than 16000 upvotes. Your next target is to reach 17000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@tensor) and compare to others on the [Steem Ranking](http://steemitboard.com/ranking/index.php?name=tensor)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @steemitboard:**
<table><tr><td><a href="https://steemit.com/steem/@steemitboard/3-years-on-steem-happy-birthday-the-distribution-of-commemorative-badges-has-begun"><img src="https://steemitimages.com/64x128/http://u.cubeupload.com/arcange/BG6u6k.png"></a></td><td><a href="https://steemit.com/steem/@steemitboard/3-years-on-steem-happy-birthday-the-distribution-of-commemorative-badges-has-begun">3 years on Steem - The distribution of commemorative badges has begun!</a></td></tr><tr><td><a href="https://steemit.com/steem/@steemitboard/happy-birthday-the-steem-blockchain-is-running-for-3-years"><img src="https://steemitimages.com/64x128/http://u.cubeupload.com/arcange/BG6u6k.png"></a></td><td><a href="https://steemit.com/steem/@steemitboard/happy-birthday-the-steem-blockchain-is-running-for-3-years">Happy Birthday! The Steem blockchain is running for 3 years.</a></td></tr></table>

###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-tensor-20190325t132538000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-03-25 13:25:39
last_update2019-03-25 13:25:39
depth1
children0
last_payout2019-04-01 13:25:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,747
author_reputation38,975,615,169,260
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,917,236
net_rshares0
@steemstem ·
$0.53
re-tensor-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190323t012609039z
<div class='text-justify'> <div class='pull-left'> <br /> <center> <img width='125' src='https://i.postimg.cc/9FwhnG3w/steemstem_curie.png'> </center>  <br/> </div> <br /> <br /> 

 This post has been voted on by the **SteemSTEM** curation team and voting trail in collaboration with **@curie**. <br /> 
 If you appreciate the work we are doing then consider [voting](https://www.steemit.com/~witnesses) both projects for witness by selecting [**stem.witness**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=stem.witness) and [**curie**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=curie)! <br /> 
For additional information please join us on the [**SteemSTEM discord**]( https://discord.gg/BPARaqn) and to get to know the rest of the community! </div>
πŸ‘  ,
properties (23)
authorsteemstem
permlinkre-tensor-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190323t012609039z
categoryutopian-io
json_metadata{"app":"bloguable-bot"}
created2019-03-23 01:26:12
last_update2019-03-23 01:26:12
depth1
children0
last_payout2019-03-30 01:26:12
cashout_time1969-12-31 23:59:59
total_payout_value0.397 HBD
curator_payout_value0.132 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length800
author_reputation262,017,435,115,313
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,769,604
net_rshares813,581,553,368
author_curate_reward""
vote details (2)
@utopian-io ·
Hey, @tensor!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-a-beginners-guide-to-dart---concurrency-asynchronous-evaluation-and-parallelism-in-dart---part-eight--20190322t122825z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-22 12:28:27
last_update2019-03-22 12:28:27
depth1
children0
last_payout2019-03-29 12:28:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length588
author_reputation152,955,367,999,756
root_title"A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,732,463
net_rshares0