Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Between Love and Hate: Coroutines

Between Love and Hate: Coroutines

Maryna Shaposhnikova

July 19, 2018
Tweet

More Decks by Maryna Shaposhnikova

Other Decks in Programming

Transcript

  1. What does it means light-weight threads? - don’t have an

    own stack - don’t map on native thread - don’t require context switching on processor conclusion : they are lighter than threads
  2. Continuation is a state of the suspended coroutine at suspension

    point CoroutineContext like a collection of thread-local variables, but… persistent
  3. start get my wizzard add to list get a user

    get a wizard suspend suspend wizard :) suspend
  4. - Do background task - Switch to UI - Show

    user launch (UI) || launch(CommonPool)
  5. UI { background { load() //suspend function } show() }

    Suspend functions are only allowed to be called from a coroutine or another suspend function.
  6. launch (UI) { launch (CommonPool) {}.join() launch (CommonPool) {}.join() launch

    (CommonPool) {}.join() } launch (UI) { async (CommonPool) {}.await() async (CommonPool) {}.await() async (CommonPool) {}.await() }
  7. launch (UI) { launch (CommonPool) { getRandomUser() getRandomUser() getRandomUser() }.join()

    } launch (UI) { withContext (CommonPool) { getRandomUser() getRandomUser() getRandomUser() } }
  8. launch (UI) { val def1 = async (CommonPool) {} val

    def2 = async (CommonPool) {} val def3 = async (CommonPool) {} def1.await() def2.await() def3.await() } joinAll(job1, job2, job3) awaitAll(job1, job2, job3) launch (UI) { val job1 = launch (CommonPool) {} val job2 = launch (CommonPool) {} val job3 = launch (CommonPool) {} job1.join() job2.join() job3.join() }