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
suspending functionの裏側
Search
Keita Kagurazaka
November 09, 2017
Programming
490
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
suspending functionの裏側
第07回Kotlin勉強会@Sansanでの発表資料です。
Keita Kagurazaka
November 09, 2017
More Decks by Keita Kagurazaka
See All by Keita Kagurazaka
三者三様 宣言的UI
kkagurazaka
0
590
SELECT FOR UPDATEの話
kkagurazaka
0
540
Mobileアプリのアーキテクチャ設計法
kkagurazaka
2
1.5k
原理から完全理解するDagger Hilt Migration
kkagurazaka
1
2k
今後のJetpackでAndroid開発はこう変わる!
kkagurazaka
16
6.5k
外部SDKのViewにマスク処理をする方法と罠
kkagurazaka
0
1.1k
AWAのフルリニューアルを支えたアーキテクチャ
kkagurazaka
1
990
CQRS Architecture on Android
kkagurazaka
7
3.2k
coroutinesで非同期ページネーション
kkagurazaka
1
720
Other Decks in Programming
See All in Programming
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
360
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
170
速習iPhone Duo対応
yuukiw00w
2
970
Simple Storage Service(S3) is not simple
iwatsukayura
0
230
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
140
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.1k
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
260
contenteditable と日本語入力に向き合う
colorful12
0
180
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
34
16k
Featured
See All Featured
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
260
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
780
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
470
Designing for Timeless Needs
cassininazir
1
520
Product Roadmaps are Hard
iamctodd
55
13k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Deep Space Network (abreviated)
tonyrice
0
360
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Building the Perfect Custom Keyboard
takai
2
900
Transcript
suspending functionの裏側 2017/11/9 第7回Kotlin勉強会@Sansan @kkagurazaka
皆さん、coroutines使ってますか?
KotlinConf Keynote Recap
KotlinConf Keynote Recap
None
coroutines じゃんじゃん使っていきましょう
そんなcoroutinesが どうやって実現されているか その裏側をお話します
自己紹介 • Keita Kagurazaka • Android App Developer • Sansan株式会社
2017/4〜 • Kotlin / CQRS / DDD / Splatoon / Github: @k-kagurazaka Twitter: @kkagurazaka
coroutinesとは • 中断・再開可能な計算のインスタンス ◦ Threadクラスのように、作成して実行する launch(UI) { val token =
fetchToken() // Tokenが取得できるまで実行を中断 val item = postItem(token, newItem) // 投稿が完了するまで実行を中断 updateUI(item) }
どのように中断・再開を 実現しているのか?
suspending function
suspending function • 呼び出されるとcoroutinesを中断する(こともできる) • suspending functionはcoroutinesかsuspending functionか らしか呼び出せない •
Java byte codeになる際にCPS transformされる
suspending function • 呼び出されるとcoroutinesを中断する(こともできる) • suspending functionはcoroutinesかsuspending functionか らしか呼び出せない •
Java byte codeになる際にCPS transformされる
Continuation Passing Style (CPS) 戻り値で値を返すのではなく、引数のContinuationインスタンスに 結果を渡すことで値を伝達するスタイル interface Continuation<in T> {
// 成功したことと、その結果を伝達 fun resume(value: T): Unit //失敗したことと、原因の例外を伝達 fun resumeWithException(exception: Throwable): Unit }
CPS transform // Kotlinでの宣言 suspend fun fetchToken(): Token // 変換後のJava
byte codeをKotlinっぽく書いたもの fun fetchToken(continuation: Continuation<Token>): Any? • 中断する場合は COROUTINE_SUSPENDED 定数を返し、 再開するときにContinuationをresumeする • 中断しない場合は結果の T を返す
CPS transform後のイメージ launch(UI) { fetchToken(object : Continuation<Token> { override fun
resume(value: Token) { postItem(value, newItem, object: Continuation<Item> { override fun resume(value: Item) { updateUI(value) } }) } }) } suspending functionの呼び出し以降がContinuationを介した コールバックに変換される
内部的な実装とはいえ コールバックのネストが 深すぎないか?
ステートマシンによる実装 class StateMachine implements Continuation<Object> { int label = 0;
// ステートマシンの状態 Token token; // suspending functionのローカル変数 Item item; // 同上 void resume(Object data) { … } }
void resume(Object data) { switch(label) { case 0: label =
1; data = fetchToken(this); // CPS変換後なのでContinuation=thisを渡す if (data == COROUTINE_SUSPENDED) break; // 中断が発生した場合は抜ける case 1: token = (Token)data; label = 2; data = postItem(token, newItem, this); if (data == COROUTINE_SUSPENDED) break; case 2: item = (Item)data; updateUI(item) label = -1; // ステートマシンの終了 break; } }
内部実装がコールバックヘル というわけではない!
まとめ • coroutineの中断はsuspending functionで発生する • suspending functionはCPS transformによってコールバック に変換される •
複数のsuspending functionの呼び出しはステートマシンにコ ンパイルされる
Thanks!