Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart is Single Threaded but why it uses Future Objects and perform asynchronous operations

In Documentation, Dart is Single Threaded but to perform two operations at a time we use future objects which work same as thread.

Use Future objects (futures) to perform asynchronous operations.

If Dart is single threaded then why it allows to perform asynchronous operations.

Note: Asynchronous operations are parallel operations which are called threads

like image 993
M.ArslanKhan Avatar asked Oct 27 '18 07:10

M.ArslanKhan


People also ask

Why Dart is single-threaded?

Because it can create separate isolate. Although within an isolate Dart again runs on a single thread. Future in Flutter or Dart gives us a promise token and says that a value will be returned at some point in future. It never says in which thread it will have its job done at that certain point.

Is Dart language single-threaded?

By design, Dart is a single-threaded programming language. That's mean we have asynchronous code across application. When a program starts, it creates something that is called Isolate.

Does Dart support multi threading?

Dart is indeed multi-threaded.

What is asynchronous programming in Dart?

Advertisements. An asynchronous operation executes in a thread, separate from the main application thread. When an application calls a method to perform an operation asynchronously, the application can continue executing while the asynchronous method performs its task.


3 Answers

You mentioned that :

Asynchronous operations are parallel operations which are called threads

First of all, Asynchronous operations are not exactly parallel or even concurrent. Its just simply means that we do not want to block our flow of execution(Thread) or wait for the response until certain work is done. But the way we implement Asynchronous operations could decide either it is parallel or concurrent.

Parallellism vs Concurrency ?

Parallelism is actually doing lots of things simultaneously at the same time. ex - You are walking and at the same time you're digesting you food. Both tasks are completely running parallel and exactly at the same time.

While

Concurrency is the illusion of Parallelism.Tasks seems to be Executed parallel but they aren't. It like handing lots of things at a time but only doing one task at a specific time. ex - You are walking and suddenly stop to tie your show lace. After tying your shoe lace you again start walking.

Now coming to Dart, Future Objects along with async and await keywords are used to perform asynchronous task. Here asynchronous doesn't means that tasks will be executed parallel or concurrent to each other. Instead in Dart even the asynchronous task is executed on the same thread which means that while we wait for another task to be completed, we will continue executing our synchronous code . Future Objects are used to represent the result of task which will be done at some time in future.

If you want to really execute your task concurrently then consider using Isolates(Which runs in separate thread and doesn't shares it memory with the main thread(or spawning thread).

like image 191
Shubhamhackz Avatar answered Nov 10 '22 02:11

Shubhamhackz


Why? Because it is a necessity. Some operations, like http requests or timers, are asynchronous in nature.

There are isolates which allow you to execute code in a different process. The difference to threads in other programming languages is that isolates do not share memory with each other (which would lead to concurrency issues), they only communicate through messages.

To receive these messages (or wrapped in a Future, the result of it), Dart uses an event loop.

  • The Event Loop and Dart
  • Are Futures in Dart threads?
like image 40
boformer Avatar answered Nov 10 '22 02:11

boformer


Here are a few notes:

  • Asynchronous doesn't mean multi-threaded. It means the code is not run at the same time. Usually asyncronous just means that it is scheduled to be run on the same thread (Isolate) after other tasks have finished.
  • Dart isn't actually single threaded. You can create another thread by creating another Isolate. However, within an Isolate the Dart code runs on a single thread and separate Isolates don't share memory. They can only communicate by messages.
  • A Future says that a value (or an error) will be returned at some point in the future. It doesn't say which thread the work is done on. Most futures are done on the current Isolate, but some futures (IO, for example) can be done on separate threads.

See this answer for links to more resources.

like image 27
Suragch Avatar answered Nov 10 '22 04:11

Suragch