Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Coroutine入門
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
curry-like
September 10, 2021
Programming
2.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Coroutine入門
curry-like
September 10, 2021
More Decks by curry-like
See All by curry-like
Kotlinで始めるBDD
currylike37
0
23k
BigQueryを用いた データ分析基盤作成入門
currylike37
0
1.4k
Other Decks in Programming
See All in Programming
RTSPクライアントを自作してみた話
simotin13
0
620
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
7.3k
New "Type" system on PicoRuby
pocke
1
990
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.4k
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
170
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
160
AI 輔助遺留系統現代化的經驗分享
jame2408
1
910
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
250
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.2k
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
140
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
350
Featured
See All Featured
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
370
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
290
The Curse of the Amulet
leimatthew05
1
13k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
How to make the Groovebox
asonas
2
2.2k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
390
Balancing Empowerment & Direction
lara
6
1.2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
580
How to build a perfect <img>
jonoalderson
1
5.7k
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
Designing Experiences People Love
moore
143
24k
Transcript
Coroutine入門 ゴール: Coroutineとはなんなのかざっくりとわかる
Coroutineとは? とても軽量なスレッドのようなもの 処理の中断・再開ができる
Gradleの設定 plugins { id 'org.jetbrains.kotlin.jvm' version '1.5.20' } group =
'me.user' version = '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2' testImplementation 'org.jetbrains.kotlin:kotlin-test' } test { useJUnitPlatform() } compileKotlin { kotlinOptions.jvmTarget = '11' } compileTestKotlin { kotlinOptions.jvmTarget = '11' }
CoroutineScope CoroutineContext Dispatcher Job CoroutineContext: Coroutineの環境を表す値のMap。どのスレッドでCoroutineを実行する かなど
CoroutineScope Coroutine Coroutine Coroutine Coroutine Coroutine Coroutine CoroutineScope: Coroutineが所属する領域 CoroutineBuilder:
launchやasyncのような
example fun main(args: Array<String>) = runBlocking { (1..5).forEach { i
-> launch { callApi(i) } } println("end") } suspend fun callApi(id: Int) { delay(1000L) println(id) } runBlocking: 現在のスレッドをラムダ内の処理が終わるまで止める launch: Coroutineの作成 delay: ライブラリが提供しているSuspend Function。Thread.sleepのようなもの suspend: Coroutineを中断する関数の修飾詞
実際にAPIを叩いてみる場合。 fun main(args: Array<String>) = runBlocking { (1..5).forEach { i
-> val result = async { callApi() } val result2 = async { callApi() } println(result.await()) } println("end") } suspend fun callApi(): String { val url = URI("https://example.com") val client = HttpClient.newBuilder().build() val handler = HttpResponse.BodyHandlers.ofString() val request = HttpRequest.newBuilder().uri(url).build() return withContext(Dispatchers.IO) { client.send(request, handler) .body() } } withContext: バックグラウンドで非同期処理をする。
種類 launch async などなど asyncは呼び出し側で使うことを推奨されているみたい。