is directly linked to the native thread in the corresponding OS ➢ It consumes a lot of memory for its stack. That’s why you cannot just create 100k threads ➢ Cooperatively multitasking ➢ Managed by a user ➢ It doesn't map on a native thread, it doesn't require context switching on a processor ➢ Coroutine in Kotlin doesn't have its own stack. So, you can create thousand of threads Thread vs Coroutines
solution RxJava Powerful, highly adopted but often misused and perceived as an overkill solution to concurrency. Coroutines Seems like a great solution but there is a need for maturation of the extensions and developer feel that a steeve learning curve awaits....
coroutines was Simula in 1967. ➢ A lightweight thread that can run a computation ➢ Non blocking code ➢ Simplify asynchronous programming by replacing callbacks ➢ It can be suspended and resume at later time
running tasks are tasks that take too long to block the main thread. 2. Main-safety allows you to ensure that any suspend function can be called from the main thread.
that is represented by an instance of CoroutineContext interface. • A coroutine itself is represented by a Job. It is responsible for coroutine’s lifecycle, cancellation, and parent-child relations. • There is also an interface called CoroutineScope that consists of a sole property — val coroutineContext: CoroutineContext
with the UI and perform light work • Calling suspend functions • Call UI functions • Updating LiveData Dispatchers.IO • Optimized for disk and network IO • off the main thread • Database • Reading/writing files • Networking Dispatchers.Default • Optimized for CPU intensive work • off the main thread • Sorting a list • Parsing JSON • DiffUtils
coroutine that is “fire and forget” — that means it won’t return the result to the caller. 2. async - builder will start a new coroutine, and it allows you to return a result with a suspend function called await. 3. runBlocking - Runs new coroutine and blocks current thread interruptible until it’s completed
and it works for you, then use RxJava • if the architecture is based on reactive stream, then use RxJava • if the codebase is contains lots of Rx operator, then use RxJava • else, use coroutines