- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathThreadPoolUtils.java
More file actions
Latest commit
47 lines (42 loc) · 1.54 KB
/
Copy pathThreadPoolUtils.java
File metadata and controls
47 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
packagecom.realmo.utils;
importjava.util.concurrent.ArrayBlockingQueue;
importjava.util.concurrent.BlockingQueue;
importjava.util.concurrent.ThreadFactory;
importjava.util.concurrent.ThreadPoolExecutor;
importjava.util.concurrent.TimeUnit;
importjava.util.concurrent.atomic.AtomicInteger;
/**
* 线程池
*/
publicclassThreadPoolUtils {
privateThreadPoolUtils(){
}
//线程池核心线程数
privatestaticintCORE_POOL_SIZE = 5;
//线程池最大线程数
privatestaticintMAX_POOL_SIZE = 100;
//额外线程空状态生存时间
privatestaticintKEEP_ALIVE_TIME = 10000;
//阻塞队列。当核心线程都被占用,且阻塞队列已满的情况下,才会开启额外线程。
privatestaticBlockingQueue<Runnable> workQueue = newArrayBlockingQueue<Runnable>(10);
//线程工厂
privatestaticThreadFactorythreadFactory = newThreadFactory() {
privatefinalAtomicIntegerinteger = newAtomicInteger();
@Override
publicThreadnewThread(Runnabler) {
returnnewThread(r, "myThreadPool thread:" + integer.getAndIncrement());
}
};
//线程池
privatestaticThreadPoolExecutorthreadPool;
static {
threadPool = newThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue,threadFactory);
}
/**
* 从线程池中抽取线程,执行指定的Runnable对象
* @param runnable
*/
publicstaticvoidexecute(Runnablerunnable){
threadPool.execute(runnable);
}
}