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
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
100
FastAPI の並行処理モデルを完全に理解する
hoto17296
8
3.2k
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
310
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
130
AIの中の人になってみる
htkym
0
140
React本体のコードリーディング
high_g_engineer
1
160
MySQLとPostgreSQLって何が違うの?
akagami
0
150
Oxlintはいいぞ(続)
yug1224
1
480
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.8k
Loosening the Reins: Go Generics Get More Flexible
kuro_kurorrr
0
350
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
1
830
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
380
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
260
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
820
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Into the Great Unknown - MozCon
thekraken
41
2.7k
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は呼び出し側で使うことを推奨されているみたい。