Coroutines basics | Coroutines for beginners

In this blog, let’s briefly discuss what Coroutines, This blog dedicated to beginners, they can learn the basic concept of coroutines, Once you understand these concepts of coroutines. You can become a master Coroutines.

Why we should use coroutines

Very first let understand why we should Coroutines an instance of thread. What is the very basic difference between thread and Coroutines? and why we want to use it instance on Thread. So let started

Thread are resource intensive

So the main problem with threads is it very resource-intensive. So it takes a lot of resources to start a thread, stop a thread, coordinate between threads, and so on. Meanwhile, Coroutines are lightweight threads Infect is very very lightweight threads.

Coroutines are lightweight threads

So we have a program where we start about millions of Coroutines and no problem in memory. If you try to start maybe a thousand threads your program will run out of memory will crash. But Coroutines are very very lightweight so you can start as many as you like with no overhead with no problem.

Ok so basically Coroutines to use pulls a thread pool is, let’s think about it as a bunch of thread that you can use whenever you like. So you have a small Coroutines which small unit of executions. Then you assign it to as thread. When the Coroutines is finishes the thread is put back in the thread pool ready to reuse by another Coroutines that need it.

Okay so they reuse threads they use lightweight functionality so you can start as many as you like with absolutely no impact on performance

Simplify async code, callback and synchronisation

Now these simplify greatly async code, So callback and synchronization are very very easy to use in Coroutines and actually they make parallel programming look very likes of sequential programming

So you can start a Coroutines code and immediately access variables and methods in your program without any overhead. And that is how easy it is to do callback from a Coroutines to your program and synchronization is also very useful, just you have 2 or 3 methods that you to synchronize between Coroutines without any overhead.

Wheres thread is very complicated and if you’ve ever worked with the thread you know what headache they can be especially when you start a lot of them need a synchronization between them.

Simple syntax

Coroutines have very very simple common syntax. So they have few concept that we will discuss in this blog, that fit together very nicely and Once you understand those concept they are very easy to use to start Coroutines and execute code in parallel. Now they can pause and resume at any time and a coroutines is not bound to a thread.

A thread simply place to execute the coroutines but the coroutines itself can be stoped at any time and restarted at any time.

So In that way they are very light weight and can be reused as many times as you like.

Coroutines basic concept

Now let go through a basic concept of Coroutines and here I’m going to list them and the upcoming article we’re going through them one by one and see the implementation with example. So let started

Scope

So the first idea is a scope. The scope is what creates and runs coroutines. It provides life cycle event so things like pausing a coroutine stopping, cancelling a coroutine, is handled by a scope.

Context

Now the context is a concept that is very tied to the scope. So the scope itself provides a context and The Context is simply the state of coroutine. Basically, It provides some variable some functionality that you can use in your coroutine. So that is a context the scope and context are very tightly tied together.

Suspending functions

Suspending function are basically functions that can be run in a coroutine or can be suspended as we call them. So basically they provide functionality that you use to run in parallel that you want o run in parallel.

Jobs

So next stuff we have jobs. Now a job is basically a handle on a coroutine and you can use a job to access the lifecycle methods of a coroutine. Okay so when you create a coroutine in a certain way which we will see in upcoming articles cancel the coroutine

You will create a job and job can we used to let say cancel the coroutine, If you choose later on next up we have a deferred which is basically a call team that returns a result.

Deferred

The next thing we have deferred is basically a call team that returns a result but sends a coroutine run in parallel. You can not return the result immediately. The result can only be only returned when the coroutine finishes its execution.

So that means we need deferred which is a future result of a coroutine and what you can do with a deferred, basically tell the program to wait for the outcome of that coroutine. when you are at a point where you need that result.

Dispatchers

Next, we dispatchers, which simply says that dispatchers manage a certain number of threads, and whenever a coroutine needs to run, it will assign it to a thread and run in time that particular thread.

So dispatcher we have just a few of them that are more or less suited for certain tasks that we will discuss in this post.

Error handling

Error handling kind of self-explanatory. It basically provides a way to handle any errors that might come up in a coroutine.

Conclusion

So that’s it these are major concepts of the coroutine. I the blog we’ll discuss these concepts in detail one by one. Once you understand the basics idea of coroutines. You can easily play around will all these concepts to give you the functionality that you require.

Estimated reading time: 5 minutes

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments