After multiple internal discussions

After multiple internal discussions as well as taking into consideration our community’s input, we are pleased to announce Ergo’s MainNet emission curve which will be applied upon launch. Detailed…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




What are COROUTINES

To overcome these challenges, developers traditionally turned to threads for handling background tasks. While threads offer a way to execute tasks concurrently, managing multiple threads can be resource-intensive and prone to errors due to out-of-memory and several other reasons.

Enter Kotlin Coroutines — a powerful and lightweight solution! Coroutines enable developers to write asynchronous code sequentially and structured, without the complexity of manually managing threads. By leveraging coroutines, developers can efficiently handle background tasks while keeping the main thread responsive, ensuring a seamless user experience.

Multiple coroutines on a single thread handling various operations

The key difference between threads and coroutines in this scenario is how they manage concurrent tasks. Threads represent a single kitchen with multiple pans, where each pan cooks one dish at a time, leading to potential delays. On the other hand, coroutines represent multiple cooking stations, enabling you to handle various tasks simultaneously and switch between them seamlessly, resulting in a more efficient cooking process and smoother customer service.

using thread
output when coroutines is used

Now implementing the same code using delay function:

delay in coroutines and sleep in thread

if we try to use delay() in place of Thread.sleep(2000) in the main thread, it will give a compilation error, as delay() is a suspend function and can only be called by coroutines and suspend functions.

This can be solved using runblocking{} + delay() , so next we will see what is run-blocking lambda and its need.

Working of runBlocking:

output when runblocking is used

INIT KOTLIN : )

Coroutines Masterclass :

Chapter-2(coroutines builders)

Chapter-3( coming soon)

Chapter-4( coming soon)

Chapter-5( coming soon)

Add a comment

Related posts:

Food Pantry Preparing for Holidays

The sustainability of the longest lasting program within The Dutchess Outreach, the Beverly Cloys Food Pantry, is a hallmark of the hardworking and selfless nature of the community in and around…

The Gray Day

A brief glimpse into the life of a migraine sufferer

Learn Exactly How I Improved WP SEO

WordPress offers great SEO website optimization tools. To get the most out of WordPress, choose and configure your applications carefully. Additional applicable SEO tactics can also be used to…