Skip to content

Instantly share code, notes, and snippets.

@gabrieljones
Created October 17, 2023 15:52
Show Gist options
  • Save gabrieljones/aecada3530ebc11cfd52d080a3ae546e to your computer and use it in GitHub Desktop.
Save gabrieljones/aecada3530ebc11cfd52d080a3ae546e to your computer and use it in GitHub Desktop.
package com.gabrieljones
import java.util.concurrent.{Executors, ScheduledFuture}
import scala.sys.ShutdownHookThread
object AllLogicInShutdownHook {
def main(args: Array[String]): Unit = {
// schedule executor
println("Adding Hook...")
val hook = sys.addShutdownHook {
println(s"${Thread.currentThread()} Starting...")
val thread = new Thread(() => {
println(s"${Thread.currentThread()} Starting...")
println(s"${Thread.currentThread()} Sleeping...")
Thread.sleep(10000)
println(s"${Thread.currentThread()} Done")
})
thread.start()
val scheduler = Executors.newScheduledThreadPool(1)
val f: ScheduledFuture[_] = scheduler.schedule({ () =>
println(s"${Thread.currentThread()} Starting...")
println(s"${Thread.currentThread()} Sleeping...")
Thread.sleep(5000)
println(s"${Thread.currentThread()} Done")
}: Runnable, 20, java.util.concurrent.TimeUnit.SECONDS)
println(s"${Thread.currentThread()} Sleeping...")
Thread.sleep(5000)
// println(s"${hook} -> ${thread} Joining...")
// thread.join()
println(s"${Thread.currentThread()} Getting $f...")
println(f.get())
println(s"${Thread.currentThread()} Got $f")
println(s"${Thread.currentThread()} Done")
}
println(s"$hook Added")
println(s"${Thread.currentThread()} Exiting...")
System.exit(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment