|
1 | 1 | package codeine.executer;
|
2 | 2 |
|
3 | 3 | import java.util.concurrent.BlockingQueue;
|
| 4 | +import java.util.concurrent.ExecutorService; |
| 5 | +import java.util.concurrent.Executors; |
4 | 6 | import java.util.concurrent.LinkedBlockingQueue;
|
| 7 | +import java.util.concurrent.ThreadFactory; |
5 | 8 | import java.util.concurrent.ThreadPoolExecutor;
|
6 | 9 | import java.util.concurrent.TimeUnit;
|
7 | 10 |
|
| 11 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 12 | + |
8 | 13 | public class ThreadPoolUtils {
|
9 | 14 |
|
10 | 15 | private static final int CAPACITY = 1000;
|
11 | 16 |
|
12 |
| - public static ThreadPoolExecutor newThreadPool(int maximumNumOfThreads){ |
| 17 | + public static ThreadPoolExecutor newThreadPool(int maximumNumOfThreads, String poolName){ |
13 | 18 | BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(CAPACITY);
|
14 |
| - ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumNumOfThreads, maximumNumOfThreads, 1, TimeUnit.SECONDS , workQueue); |
| 19 | + ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumNumOfThreads, maximumNumOfThreads, 1, TimeUnit.SECONDS , workQueue, createFactory(poolName)); |
15 | 20 | threadPoolExecutor.allowCoreThreadTimeOut(true);
|
16 | 21 | return threadPoolExecutor;
|
17 | 22 | }
|
| 23 | + //TODO I think it is better to use this version and eliminate the other one |
| 24 | + //need to check more about allowCoreThreadTimeOut and assert Error id above amount of tasks |
| 25 | + public static ExecutorService newFixedThreadPool(int concurrency, String poolName) { |
| 26 | + return Executors.newFixedThreadPool(concurrency,createFactory(poolName)); |
| 27 | + } |
| 28 | + private static ThreadFactory createFactory(String poolName) { |
| 29 | + return new ThreadFactoryBuilder().setNameFormat(poolName+"-%d").build(); |
| 30 | + } |
18 | 31 | }
|
0 commit comments