Created
October 17, 2023 15:52
-
-
Save gabrieljones/aecada3530ebc11cfd52d080a3ae546e 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 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