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
Coroutine入門
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
The Rails Doctrine Decade
koic
2
550
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
460
気持ちぃ〜角丸 Squircle
ryu1013
0
220
残高管理から台帳サービスへの進化
artoy
0
180
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
setup-vp GitLab対応の裏側
naokihaba
0
160
速習iPhone Duo対応
yuukiw00w
2
980
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
150
Simple Storage Service(S3) is not simple
iwatsukayura
0
250
すこし踏み込む CancellationToken
htkym
2
1.7k
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
160
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
760
Featured
See All Featured
Claude Code のすすめ
schroneko
67
230k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Deep Space Network (abreviated)
tonyrice
0
360
Become a Pro
speakerdeck
PRO
31
6.3k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
720
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Optimizing for Happiness
mojombo
378
71k
How to train your dragon (web standard)
notwaldorf
96
6.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Facilitating Awesome Meetings
lara
57
7.2k
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は呼び出し側で使うことを推奨されているみたい。