
io.p8e.util.ThreadPoolFactory.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of p8e-common Show documentation
Show all versions of p8e-common Show documentation
A collection of services and libraries that iteract and run Provenance Java based contracts.
The newest version!
package io.p8e.util
import com.google.common.util.concurrent.ThreadFactoryBuilder
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import kotlin.concurrent.thread
object ThreadPoolFactory {
fun newFixedThreadPool(size: Int, namingPattern: String): ExecutorService {
return Executors.newFixedThreadPool(size, ThreadFactoryBuilder().setNameFormat(namingPattern).build())
.apply {
Runtime.getRuntime().addShutdownHook(
thread(start = false) {
shutdown()
}
)
}
}
fun newScheduledThreadPool(size: Int, namingPattern: String): ScheduledExecutorService {
return Executors.newScheduledThreadPool(size, ThreadFactoryBuilder().setNameFormat(namingPattern).build())
.apply {
Runtime.getRuntime().addShutdownHook(
thread(start = false) {
shutdown()
}
)
}
}
fun newFixedDaemonThreadPool(size: Int, namingPattern: String): ExecutorService {
return Executors.newFixedThreadPool(size, ThreadFactoryBuilder().setDaemon(true).setNameFormat(namingPattern).build())
.apply {
Runtime.getRuntime().addShutdownHook(
thread(start = false) {
shutdown()
}
)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy