feat: add thread pool factory

Signed-off-by: MiaoWoo <admin@yumc.pw>
backup
MiaoWoo 2020-06-17 18:28:12 +08:00
parent b98409abe7
commit 5a9e33e695
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
const Thread = Java.type('java.lang.Thread')
const ThreadGroup = Java.type("java.lang.ThreadGroup")
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
const LinkedBlockingQueue = Java.type("java.util.concurrent.LinkedBlockingQueue")
interface ThreadPoolConfig {
corePoolSize?: number
maximumPoolSize?: number
keepAliveTime?: number
workQueueSize?: number
groupName: string
}
export default {
create(config: ThreadPoolConfig): java.util.concurrent.ThreadPoolExecutor {
let threadCount = new AtomicInteger(0)
config = {
corePoolSize: 10,
maximumPoolSize: 100,
keepAliveTime: 60,
workQueueSize: 500,
...config
}
return new ThreadPoolExecutor(
config.corePoolSize, config.maximumPoolSize, config.keepAliveTime, Packages.java.util.concurrent.TimeUnit.SECONDS,
new LinkedBlockingQueue(config.workQueueSize),
(run: any) => new Thread(new ThreadGroup(config.groupName), run, config.groupName + "-" + threadCount.incrementAndGet()),
new ThreadPoolExecutor.CallerRunsPolicy()
)
}
}