Skip to content

Instantly share code, notes, and snippets.

@latant
Created October 23, 2020 10:50
Show Gist options
  • Select an option

  • Save latant/54ed54f21565072d7259aa629db853fe to your computer and use it in GitHub Desktop.

Select an option

Save latant/54ed54f21565072d7259aa629db853fe to your computer and use it in GitHub Desktop.
A coroutine function that suspends until a shutdown is requested
import kotlin.concurrent.thread
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
suspend fun awaitShutdown() {
suspendCoroutine<Unit> { c ->
Runtime.getRuntime().addShutdownHook(thread(start = false) {
c.resume(Unit)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment