DEV Community

kumaraish
kumaraish

Posted on

Coroutines for a 5 year old

Assuming the 5 year old already knows Threads ;-)

A coroutine is an instance of suspendable computation. It is conceptually similar to a thread, in the sense that it takes a block of code to run that works concurrently with the rest of the code. However, a coroutine is not bound to any particular thread. It may suspend its execution in one thread and resume in another one.

That was from https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine

Let’s translate that.

There is a long street with banks, offices, shops, restaurants, schools along the way. You and your neighbors and friends and family's and perfect strangers, all of you living at that street have a job.

Now you start from home and take a Taxi. Midway, you have to stop at the bank to draw some cash. You hop off the taxi, because you don’t want to keep it waiting. Taxi is now free to give a ride to some one else with a job.

Once you are finished with your work in bank, you come out, wave at another Taxi that was waiting nearby and move along.

Coroutines for a 5 year old

You all are Coroutines. Taxis are Threads.

Top comments (0)