All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.p8e.util.ThreadPoolFactory.kt Maven / Gradle / Ivy

Go to download

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