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
curry-like
September 10, 2021
Programming
2.4k
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
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
730
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
140
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
220
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
670
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
450
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
370
霧の中の代数的エフェクト
funnyycat
1
480
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
160
5分で問診!Composer セキュリティ健康診断
codmoninc
0
900
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
310
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
140
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
200
We Have a Design System, Now What?
morganepeng
55
8.3k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
490
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
290
The Limits of Empathy - UXLibs8
cassininazir
1
590
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
Unsuck your backbone
ammeep
672
58k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
230
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
230
The Curse of the Amulet
leimatthew05
2
14k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
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は呼び出し側で使うことを推奨されているみたい。