Skip to content

Instantly share code, notes, and snippets.

@rahul619anand
Created May 1, 2017 05:25
Show Gist options
  • Save rahul619anand/eeb4c024bbfb28344fb8e1858a245530 to your computer and use it in GitHub Desktop.
Save rahul619anand/eeb4c024bbfb28344fb8e1858a245530 to your computer and use it in GitHub Desktop.
Simple Scala Executor with Custom Thread Names and Thread StackTrace
import java.util.concurrent.Executors
import com.google.common.util.concurrent.ThreadFactoryBuilder
/**
* Created by ranand on 4/26/2017 AD.
*/
object TestThread {
def main(args: Array[String]): Unit = {
val threadFactory =new ThreadFactoryBuilder()
.setUncaughtExceptionHandler((t: Thread, e: Throwable) => {
println(":::::::: Exception in thread :::::::::: '" + t.getName() + "'", e)
})
.setNameFormat("TEST-%d")
.build();
val executorPool = Executors.newFixedThreadPool(2,threadFactory)
for (i <- 1 to 5) {
executorPool.execute(new Thread(() => {
println(
/*" Id : " + Thread.currentThread().getId +*/
"===== THREAD Name : " + Thread.currentThread().getName +
", Group : " + Thread.currentThread().getThreadGroup //+
/*", State : " + Thread.currentThread().getState*/)
if (i == 1) throw new Exception
Thread.sleep(5000)
}))
}
println(Thread.getAllStackTraces().keySet())
Thread.sleep(2000)
println ("$$$$$$$ Wait 2 sec $$$$$$$")
println(Thread.getAllStackTraces().keySet())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment