knthmn
09/25/2021, 4:57 AMfun main() = runBlocking {
// ... launch some stuffs
// wait for ctrl-C, how?
cleanup()
cancel()
}
Rob Elliot
09/25/2021, 10:06 AMRuntime.getRuntime().addShutdownHook(
object : Thread() {
override fun run() {
cleanup()
cancel()
}
}
)
With a CountDownLatch you can optionally move the cleanup back into main:
fun blockUntilShutdown(
gate: Gate = Gate.closed()
) {
val gate = CountDownLatch(1)
val runningThread = Thread.currentThread()
Runtime.getRuntime().addShutdownHook(
object : Thread() {
override fun run() {
gate.countDown()
runningThread.join()
}
}
)
gate.await()
}
fun main() = runBlocking {
// ... launch some stuffs
blockUntilShutdown()
cleanup()
cancel()
}
Not sure how that all plays with coroutines, mind you…Rob Elliot
09/25/2021, 10:12 AMSignal.handle(Signal("INT")) { signal ->
println("handling $signal")
}
but obviously depending on the sun.misc
package is suboptimal, not being part of the standard.