Kotlin Coroutines for Android Developer

 In computer science, two types of multitasking methods manage multiple concurrent processes.

  • In one type the operating system controls the switch between processes.
  • The other type is called cooperative multitasking, in which processes control their behavior by themselves.

Coroutines are software components that create subroutines for cooperative multitasking.

Coroutines were first used in 1958 for the assembly language. Developers of modern programming languages such as Python, javascript, and c# have been using coroutines for many years.

 


In Kotlin a coroutine can be introduced as a sequence of well-managed “sub-tasks”. To some extent, a coroutine can be considered a lightweight thread.

You can execute many coroutines in a single thread. A coroutine can also be switched between threads.

That means a coroutine can be suspended from one thread and resumed from another thread.


As android developers, with the release of kotlin 1.3, we now have a fully stable coroutines api.

All the painful multithreading tasks are done with rxjava, async task,s, or other methods such as executors, HandlerThreads, and IntentServices can be easily and efficiently done with Coroutines.

Coroutines API also allows us to write asynchronous codes in a sequential manner.


Hence it avoids unnecessary boilerplate codes that come with callbacks and makes our codes more readable and maintainable.


Before coding, our first coroutine, let’s very quickly discuss 

why we need asynchronous programming in android development

Most smartphones have a refresh frequency of at least 60hz. For 1 second or for 1000 milliseconds,

the app refreshes 60 times.


Let’s divide 1000 milliseconds by 60.

It is 16.666… repeating


So, if our app runs on this phone it has to draw the screen using the main thread approximately every 16 milliseconds.


But we already have better smartphones available in the market with higher refresh rates such as 90hz,  120hz, and more advance.


If the phone has a refreshing level of 90hz, Let’s divide 1000 milliseconds by 90 our app has only 11 ms to perform tasks in the Android main thread.


If the refresh level is 120hz, 1000 divided by 120 our app will have only 8 ms. By default android main thread has a set of regular responsibilities.


It has to always parse XML, inflate view components and draw them again and again for every refresh.

The main thread also has to deal with user interactions such as click events.


So, If we add more tasks to the main thread if its execution time exceeds this super small time gap between two refreshes, the app will show performance errors. The screen might freeze. Users will see unpredictable behaviors in view components. It will even cause an ANR application not to respond error.

As a result of technological advancements, these refresh rates getting higher and higher every year.

Therefore, as android developers, we should always try to execute long-running tasks asynchronously in a separate thread.

To achieve that, the newest, most efficient, and most effective technology we have today is Kotilin Coroutines.


Creating too many threads can actually make an application underperform in some situations; threads are objects which impose overhead during object allocation and garbage collection.

To overcome these issues, Kotlin introduced a new way of writing asynchronous, non-blocking code; the Coroutine.

Similar to threads, coroutines can run concurrently, wait for, and communicate with each other with the difference that creating them is way cheaper than threads.


Threads vs. Coroutines Notes:

  • Coroutines are Light-weight Threads
  • Like threads, coroutines can run in parallel, wait for each other, and communicate with each other.
  • Coroutines != Thread.
  • Coroutines are very very cheap - almost free
  • You can create thousands of them without any memory leak, So Coroutines is best for modern applications




Suspend function:
  • A function with a 'suspend' modifier is known as suspending function.
  • Suspend function 'delay' should be called only from a coroutine or another suspend function
  • They cannot be called from outside  a coroutine
..

Comments