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

8,675

Solution 1

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).

Solution 2

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.

Solution 3

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.

Solution 4

Dart is single threaded, but it can call native code(like c/c++) to perform asynchronous operations, which can introduce new thread.

In Flutter, Flutter engine is implement in c++, which provide the low-level implementation of Flutter’s core API, including asynchronous tasks like file and network I/O through new thread underneath. enter image description here

Like Dart, JavaScript is also single threaded, I find this video very helpful to understand "Single Threaded" thing. what the heck is event loop

Share:
8,675
M.ArslanKhan
Author by

M.ArslanKhan

Updated on December 07, 2022

Comments

  • M.ArslanKhan
    M.ArslanKhan over 1 year

    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