Skip to content

Commit

Permalink
kotlinify thread pool (#18366)
Browse files Browse the repository at this point in the history
chore: Kotlinify thread pool
  • Loading branch information
OmarBasem authored and briansztamfater committed Jan 11, 2024
1 parent 278af1c commit 3331a31
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package im.status.ethereum.module

import java.util.concurrent.*

/**
* Uses an unbounded queue but allows timeout of core threads
* (modified case 2 in
* https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html )
*/
class StatusThreadPoolExecutor private constructor() {
private val NUMBER_OF_CORES: Int = Runtime.getRuntime().availableProcessors()
private val THREADS_TO_CORES_RATIO: Int = 100
private val KEEP_ALIVE_TIME: Int = 1
private val KEEP_ALIVE_TIME_UNIT: TimeUnit = TimeUnit.SECONDS

private val mQueue: BlockingQueue<Runnable> = LinkedBlockingQueue()
private val mThreadPool: ThreadPoolExecutor

init {
mThreadPool = ThreadPoolExecutor(
THREADS_TO_CORES_RATIO * NUMBER_OF_CORES,
THREADS_TO_CORES_RATIO * NUMBER_OF_CORES,
KEEP_ALIVE_TIME.toLong(),
KEEP_ALIVE_TIME_UNIT,
mQueue
)

// Allow pool to drain
mThreadPool.allowCoreThreadTimeOut(true)
}

/** Singleton holder */
private object Holder {
val instance = StatusThreadPoolExecutor()
}

companion object {
@JvmStatic
fun getInstance(): StatusThreadPoolExecutor {
return Holder.instance
}
}

fun execute(r: Runnable) {
mThreadPool.execute(r)
}
}

0 comments on commit 3331a31

Please sign in to comment.