Created
June 3, 2022 14:44
-
-
Save myungpyo/e0725e4c31abb386df5bcd4e000c5e30 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object ExceptionTester3 { | |
private val singleThreadedDispatcher = Executors.newSingleThreadExecutor { runnable -> | |
Thread(runnable).apply { | |
// Set uncaught exception handler for the thread | |
// setUncaughtExceptionHandler { _, exception -> println("UncaughtExceptionHandler : $exception") } | |
} | |
}.asCoroutineDispatcher() | |
private val testingScope = CoroutineScope(singleThreadedDispatcher) | |
@JvmStatic | |
fun main(args: Array<String>) = runBlocking { | |
// Set uncaught exception handler for all threads | |
// Thread.setDefaultUncaughtExceptionHandler { thread, exception -> println("DefaultUncaughtExceptionHandler : $exception") } | |
singleThreadedDispatcher.use { | |
with(testingScope) { | |
executeTest().join() | |
} | |
} | |
} | |
private fun CoroutineScope.executeTest(): Job { | |
val exceptionHandler = CoroutineExceptionHandler { _, exception -> | |
println("CoroutineExceptionHandler : $exception") | |
} | |
// Set coroutine exception handler | |
return launch(CoroutineName("Coroutine:A") /* + exceptionHandler */) { | |
launch(CoroutineName("Coroutine:B")) { | |
launch(CoroutineName("Coroutine:C-1")) { | |
repeat(3) { | |
delay(1000) | |
log("Processing : ${it + 1} / 3") | |
} | |
} | |
launch(CoroutineName("Coroutine:C-2")) { | |
repeat(6) { | |
delay(500) | |
log("Processing : ${it + 1} / 5") | |
if (it > 0) throw RuntimeException("Something wrong!") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment