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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
210
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
550
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
300
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
160
Webの地図
yosuke_furukawa
PRO
6
4.7k
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
130
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
200
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.1k
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
280
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2.3k
JRuby: Past, Present, and Future
headius
0
160
AI活用は、個人から組織へ|マルチプレイヤーエージェントハーネス「QM」の社内活用事例 / AI use is moving from individuals to orgs
rkaga
1
120
Featured
See All Featured
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
910
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Deep Space Network (abreviated)
tonyrice
0
310
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
700
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
How to Ace a Technical Interview
jacobian
281
24k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Thoughts on Productivity
jonyablonski
76
5.4k
The Language of Interfaces
destraynor
162
27k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
Facilitating Awesome Meetings
lara
57
7.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
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は呼び出し側で使うことを推奨されているみたい。