.yass.yass.60.0.1.source-code.ThreadFactory.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yass Show documentation
Show all versions of yass Show documentation
Yet Another Service Solution
package ch.softappeal.yass
import java.lang.Thread.UncaughtExceptionHandler
import java.util.Date
import java.util.concurrent.ThreadFactory
import java.util.concurrent.atomic.AtomicInteger
val StdErr = UncaughtExceptionHandler { thread, throwable ->
try {
System.err.println("### UncaughtExceptionHandler at '${Date()}' in thread '${thread?.name}' ###")
throwable?.printStackTrace()
} finally {
if (throwable !is Exception) System.exit(1)
}
}
val Terminate = UncaughtExceptionHandler { thread, throwable ->
try {
StdErr.uncaughtException(thread, throwable)
} finally {
System.exit(1)
}
}
@JvmOverloads
fun namedThreadFactory(
name: String, uncaughtExceptionHandler: UncaughtExceptionHandler, priority: Int = Thread.NORM_PRIORITY, daemon: Boolean = false
): ThreadFactory {
val number = AtomicInteger(1)
return ThreadFactory { r ->
val thread = Thread(r, "$name-${number.getAndIncrement()}")
thread.uncaughtExceptionHandler = uncaughtExceptionHandler
if (thread.priority != priority) thread.priority = priority
if (thread.isDaemon != daemon) thread.isDaemon = daemon
thread
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy