Created
August 31, 2022 07:31
-
-
Save myungpyo/c1193abb532b3b78499d7ee1435f1b99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo.bar | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
import kotlinx.coroutines.* | |
private val testingScope = CoroutineScope(CoroutineName("TestingScope")) | |
fun main() = runBlocking { | |
with(testingScope) { | |
val mainJob = executeTest() | |
mainJob.join() | |
} | |
} | |
private fun CoroutineScope.executeTest(): Job = | |
launch(CoroutineName("Coroutine:A")) { | |
launch(CoroutineName("Coroutine:B-1")) { | |
repeat(5) { | |
delay(500) | |
log("Processing : ${it + 1} / 5") | |
} | |
log("Complete") | |
} | |
// 아래 주석 해제 시 C-2의 예외가 A까지 전파 안됨 | |
//launch(CoroutineName("Coroutine:B-2") + SupervisorJob()) { | |
// 아래 주석 해제 시 C-2의 예외가 A까지 전파 안됨, Job의 연결은 되었지만 SuperviorJob이므로. | |
//launch(CoroutineName("Coroutine:B-2") + SupervisorJob(coroutineContext[Job])) { | |
// 아래 주석 해제 시 C-2의 예외가 A까지 전파 안됨 | |
//launch(CoroutineName("Coroutine:B-2") + Job()) { | |
// 아래 주석 해제 시 C-2의 예외가 A까지 전파 됨, Job이 연결되었으므로. | |
//launch(CoroutineName("Coroutine:B-2") + Job(coroutineContext[Job])) { | |
launch(CoroutineName("Coroutine:B-2")) { | |
launch(CoroutineName("Coroutine:C-1")) { | |
repeat(3) { | |
delay(500) | |
log("Processing : ${it + 1} / 3") | |
} | |
log("Complete") | |
} | |
launch(CoroutineName("Coroutine:C-2")) { | |
repeat(6) { | |
delay(200) | |
log("Processing : ${it + 1} / 6") | |
if (it > 0) throw RuntimeException("Something wrong!") | |
} | |
log("Complete") | |
} | |
} | |
} | |
fun CoroutineScope.log(message: String) { | |
println("${coroutineContext[CoroutineName]} : $message") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment