-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
47 additions
and
44 deletions.
There are no files selected for viewing
44 changes: 0 additions & 44 deletions
44
...tive-status/android/src/main/java/im/status/ethereum/module/StatusThreadPoolExecutor.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
...native-status/android/src/main/java/im/status/ethereum/module/StatusThreadPoolExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |