coroutines

suspend fun fetchValue(): Int = 42

A suspend function can pause without blocking the thread That is the difference between a blocking call and an asynchronous one

launch and async

launch starts a coroutine and returns a job async starts one and returns a deferred value Use async when you need the result

Structured concurrency

Coroutines are scoped A parent waits for children If the parent is cancelled, the children are cancelled That is the feature that makes coroutines safer than raw threads

Dispatchers

  • Dispatchers.Default for CPU bound work
  • Dispatchers.IO for blocking IO
  • Dispatchers.Main for UI work

Do not run network calls on the default dispatcher They block the threads that other coroutines need

Next

Interop in 09 interop

Test this in your lab first

Posted