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

com.firefly.kotlin.ext.common.CommonCoroutinePool.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.kotlin.ext.common

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.CoroutineContext

/**
 * @author Pengtao Qiu
 */
object CommonCoroutinePool : CoroutineDispatcher() {

    val defaultPoolSize: Int = Integer.getInteger(
        "com.firefly.kotlin.common.async.defaultPoolSize",
        Runtime.getRuntime().availableProcessors()
                                                 )

    private val pool: ForkJoinPool = ForkJoinPool(defaultPoolSize, { pool ->
        val worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool)
        worker.name = "firefly-kt-async-pool-" + worker.poolIndex
        worker
    }, null, true)

    override fun dispatch(context: CoroutineContext, block: Runnable) {
        pool.execute(block)
    }

}

object CoroutineDispatchers {
    val computation: CommonCoroutinePool by lazy { CommonCoroutinePool }
    val ioBlocking: CoroutineDispatcher by lazy {
        val threadId = AtomicInteger()
        ThreadPoolExecutor(
            16, 64,
            30L, TimeUnit.SECONDS,
            ArrayBlockingQueue(10000)
                          ) { r ->
            Thread(r, "firefly-kt-io-blocking-pool-" + threadId.getAndIncrement())
        }.asCoroutineDispatcher()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy