coroutineScope { launch { for (i in 1..5) { println("Hello A $i thread:${Thread.currentThread()}") } } launch { for (i in 1..5) { println("Hello B $i thread:${Thread.currentThread()}") } } launch { for (i in 1..5) { println("Hello C $i thread:${Thread.currentThread()}") } } }
Hello A 1 thread:Thread[main,5,main] Hello A 2 thread:Thread[main,5,main] Hello A 3 thread:Thread[main,5,main] Hello A 4 thread:Thread[main,5,main] Hello A 5 thread:Thread[main,5,main] Hello B 1 thread:Thread[main,5,main] Hello B 2 thread:Thread[main,5,main] ・・・ Hello C 1 thread:Thread[main,5,main] Hello C 2 thread:Thread[main,5,main] ・・・ Aの1〜5→Bの1〜5→Cの1〜5 の順番で出力される
runBlocking { launch { for (i in 1..5) { delay(1) // 中断 println("Hello A $i thread:${Thread.currentThread()}") } } launch { for (i in 1..5) { delay(1) // 中断 println("Hello B $i thread:${Thread.currentThread()}") } } launch { for (i in 1..5) { delay(1) // 中断 println("Hello C $i thread:${Thread.currentThread()}") } } }
Hello A 1 thread:Thread[main,5,main] Hello B 1 thread:Thread[main,5,main] Hello C 1 thread:Thread[main,5,main] Hello A 2 thread:Thread[main,5,main] Hello B 2 thread:Thread[main,5,main] Hello C 2 thread:Thread[main,5,main] Hello A 3 thread:Thread[main,5,main] Hello B 3 thread:Thread[main,5,main] ・・・ Aの1→Bの1→Cの1→Aの2→Bの2・・・ と出力される
runBlocking { launch { for (i in 1..5) { delay(1) // 中断 println("Hello A $i thread:${Thread.currentThread()}") } } launch { for (i in 1..5) { delay(1) // 中断 println("Hello B $i thread:${Thread.currentThread()}") } } launch { for (i in 1..5) { delay(1) // 中断 println("Hello C $i thread:${Thread.currentThread()}") } } } ①中断して次のlaunchへ ②中断して次のlaunchへ ③中断して次のlaunchへ ④復帰して出力 ⑥復帰して出力 ⑧復帰して出力 ⑤中断して次のlaunchへ ⑦中断して次のlaunchへ
implementation("org.springframework.boot:spring-boot-starter-data-r2dbc") runtimeOnly("dev.miku:r2dbc-mysql") Gradleに依存関係の追加 Spring Bootのstarterと、MySQLのドライバーを追加する
interface UserRepository : CoroutineCrudRepository { } データクラスとRepositoryの作成 data class User( @Id val id: Int, val name: String, val age: Int ) ● テーブルに構造に紐づいたデータクラスを作成 ● CoroutineCrudRepositoryを実装したRepositoryを作成(これで各 種アクセスの関数が使える )
/** * Retrieves an entity by its id. * * @param id must not be null. * @return [Mono] emitting the entity with the given id or empty if none found. * @throws IllegalArgumentException in case the given id is null. */ suspend fun findById(id: ID): T? /** * Returns whether an entity with the given id exists. * * @param id must not be null. * @return true if an entity with the given id exists, false otherwise. * @throws IllegalArgumentException in case the given id is null. */ suspend fun existsById(id: ID): Boolean /** * Deletes the entity with the given id. * * @param id must not be null. * @throws IllegalArgumentException in case the given id is null. */ suspend fun deleteById(id: ID)
interface UserRepository : CoroutineCrudRepository { suspend fun findByName(name: String): List @Query( """ SELECT * FROM user ORDER BY age LIMIT 1 """ ) suspend fun findMostYoung(): User } ● シンプルなクエリは、 findByNameなど機能とカラム名の命名規則で作れる ● @Queryを使うことで自由にクエリを定義することもできる ● これもノンブロッキングになる