1 public class CommonThreadPool {
2 private static final CommonThreadPool pool = new CommonThreadPool();
3 private ThreadPoolExecutor executor;
4 private CommonThreadPool() {
5 int corePoolSize = 4;
6 int maximumPoolSize = 4;
7 int workQueue = 500;
8 executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 30000L, TimeUnit.MILLISECONDS,
9 new LinkedBlockingQueue<>(workQueue),
10 new ThreadPoolExecutor.DiscardPolicy());
11 executor.allowCoreThreadTimeOut(true);
12 }
13
14 public static CommonThreadPool getInstance() {
15 return pool;
16 }
17
18 /**
19 * 添加带返回结果的任务(具体的线程任务需实现Callable接口)
20 */
21 public <M> Future <M> add(Callable<M> task) {
22 return executor.submit(task);
23 }
24
25 /**
26 * 添加无返回结果的任务(具体的线程任务需实现Runnable接口)
27 */
28 public void add(Runnable task) {
29 executor.execute(task);
30 }
31 }