Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Between Love and Hate: Coroutines
Search
Maryna Shaposhnikova
July 19, 2018
Programming
110
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Between Love and Hate: Coroutines
Maryna Shaposhnikova
July 19, 2018
More Decks by Maryna Shaposhnikova
See All by Maryna Shaposhnikova
Mobile Platform Team. Grab.
marinameier
0
370
Other Decks in Programming
See All in Programming
The Rails Doctrine Decade
koic
2
100
スマート反転とウェブアクセシビリティ
camiha
0
210
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
1
150
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3k
Webの地図
yosuke_furukawa
PRO
6
4.6k
Family mrubyの進捗
kishima
1
130
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
290
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
410
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
230
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
120
WebAssembly in Android Apps 〜 WASMはJNIの夢を見るか
keiji
1
110
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.1k
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
187
23k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
660
The Invisible Side of Design
smashingmag
301
52k
What's in a price? How to price your products and services
michaelherold
247
13k
The Language of Interfaces
destraynor
162
27k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
540
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
280
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
Transcript
Between Love and Hate: Coroutines Maryna Shaposhnikova Android dev Glindev
Languages:
There are two types of Coroutines Stackfull Stackless
None
Why we need it? asynchronous, non-blocking code light-weight threads
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
How it works? Callback?!?
State machine - if-conditions - switch structures.
None
Continuation is a state of the suspended coroutine at suspension
point CoroutineContext like a collection of thread-local variables, but… persistent
start get my wizzard add to list get a user
get a wizard suspend suspend wizard :) suspend
- Do background task - Switch to UI - Show
user launch (UI) || launch(CommonPool)
UI { background { load() //suspend function } show() }
Suspend functions are only allowed to be called from a coroutine or another suspend function.
launch {} async {} Job Deferred aka promise join() await()
w/o result with result
Compare
launch (UI) { launch (CommonPool) {}.join() launch (CommonPool) {}.join() launch
(CommonPool) {}.join() } launch (UI) { async (CommonPool) {}.await() async (CommonPool) {}.await() async (CommonPool) {}.await() }
withContext() ONE or MANY?
launch (UI) { launch (CommonPool) { getRandomUser() getRandomUser() getRandomUser() }.join()
} launch (UI) { withContext (CommonPool) { getRandomUser() getRandomUser() getRandomUser() } }
= 3000 ms 1000 ms 1000 ms 1000 ms
= 1000 ms
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() }
None
Exception
suspendCoroutine callback { callback { callback { callback { print(‘I
hate callback hell”) } } } }
None
Retrofit?
None
- Build abstraction - Use Channels for modularization - Avoid
to use CommonPool - Avoid to use UI
Could coroutines replace Rx?
None
Thank you! Maryna Shaposhnikova Android dev Glindev Sample project https://github.com/MarinaShaposhnikova/M
agicCoroutines