What actually Isolate is?

TheBlacktaler
4 min readFeb 4, 2024

--

Photo by Sander Sammy on Unsplash

Is there any understandable post about that? I can not UNDERSTAND something from those documented posts without examples! I need more complicated and more simple explanation to understand that!! I need to understand WHAT THE FUN the isolate is ?!

First of all we need to understand how our main function and async works. Because there might be some questions about the difference between Future and Isolate. So I decided to divide them into some steps.

Step 1. Understanding async and sync(Futures).

Of course there might be many of the posts which explained async very well, but anyway this post should have the full explanation.

1.1. Async.

The full form of async is asynchronous.

Well, everyone in the childhood might have played running in the streets with other children, and in the running the one who runs faster, bypasses slower ones. In async also the function which is executed faster, bypasses other ones and is executed earlier.

Asynchronous running
Functions being executed in asynchronous mode

1.2. Sync.

In the sync(synchronous) method/function, it is the opposite of async. Every event has it’s turn. In order to have sync function we need to add async keyword, then await keyword should be put on a function which needs to be waited.

Photo by Ewan Yap on Unsplash

Step 2. Understanding isolate:

Well If you are experiencing this situation you are one of many, just like me.
So before beginning the topic we need to clarify one thing. You might think that what is the difference between async and Isolate after having heard about isolate. Because there is async ability which allows us to work asynchronously, with or without waiting the other tasks. But it does not allow us to perform multiple tasks at once.

Imagine you are a driver, and there is one line on the road. And you can only drive in one line, if there are cars before you, you can’t change the line, you can just bypass them, but anyway you will drive in that line. Anyways if there are a lot of cars, traffic jams might happen. In this case, additional lines might help to solve the problem. And that’s why we have Isolate, in order to add more lines(not in real life, but in code :).

Photo by Clay Banks on Unsplash

And this is our regular code which starts with main method.
And in order to add async function here, you can bypass a car which is driving too slow, but two cars can’t drive side by side simultaneously.

If we add(spawn) isolate , we add another line on the road, we can add as many lines(isolates) as we can. So there will be multiple lines to ride at once, but it will not increase speed of the cars(tasks), just the line is divided into multiple lines.

Photo by Markus Spiske on Unsplash

It’s recommended to add up to 4 or 5 isolates, because the isolate depends on the platform threads. One isolate approximately takes one thread or half of a core. So if you use many isolates in order to improve the performance of the app, it will be bad for the OS and the OS might limit the performance of your app, in order to prevent it influencing to the system. Therefore, no app needs to perform that much task at once.

If you have known java or kotlin, it’s the same thing with threads but it just differs with it’s name “Isolate”. Every isolate is isolated from other isolates, so it can’t read memory and or variables of other isolates directly. So by imagine we can say, there might be multiple lines on the road. But the cars of one line can’t switch to the other line. In addition, car drivers can’t get information or communicate with the drivers of other lines.

Therefore, Isolates have their own event loop. There might be a question that, why actually is it called event loop?! And how it works? In order to make it more intelligible, we can see the example:

So here in the example, every method/function is executed one by one. So the main function(which is main isolate) executes events one by one in one loop. So the main method executes a loop, and that loop executes every task which is added to the queue. If we take the example above, first event is added first, the second one is added second and so on.

As flutter documentation says:

All Dart code runs in isolates, which are similar to threads, but differ in that isolates have their own isolated memory. They do not share state in any way, and can only communicate by messaging. By default, Flutter apps do all of their work on a single isolate — the main isolate. In most cases, this model allows for simpler programming and is fast enough that the application’s UI doesn’t become unresponsive.

Step 3: Working process of isolates is coming soon, don’t forget to subscribe if the topic has been interesting.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

TheBlacktaler
TheBlacktaler

Written by TheBlacktaler

I'm going to publish more posts. In real life I'm a happy, and simultaneously serious person and I'm a junior professional) :

No responses yet

Write a response