MCPcopy Create free account
hub / github.com/aofeng/threadpool4j / ThreadPoolImpl

Class ThreadPoolImpl

src/cn/aofeng/threadpool4j/ThreadPoolImpl.java:32–293  ·  view source on GitHub ↗

多线程池。 @author 聂勇

Source from the content-addressed store, hash-verified

30 * @author <a href="mailto:aofengblog@163.com">聂勇</a>
31 */
32public class ThreadPoolImpl implements ILifeCycle, ThreadPool {
33
34 /** 默认的线程池名称 */
35 private static final String DEFAULT_THREAD_POOL = "default";
36
37 private static Logger _logger = LoggerFactory.getLogger(ThreadPoolImpl.class);
38
39 protected ThreadPoolConfig _threadPoolConfig = new ThreadPoolConfig();
40 protected int _status = ThreadPoolStatus.UNINITIALIZED;
41
42 Map<String, ExecutorService> _multiThreadPool = new HashMap<String, ExecutorService>();
43 ThreadPoolStateJob _threadPoolStateJob;
44 ThreadStateJob _threadStateJob;
45 ThreadStackJob _threadStackJob;
46
47 public ThreadPoolImpl() {
48 // nothing
49 }
50
51 @Override
52 public void init() {
53 if (ThreadPoolStatus.UNINITIALIZED != _status) {
54 _logger.warn("initialization thread pool failed, because the status was wrong, current status was {} (0:UNINITIALIZED, 1:INITIALITION_SUCCESSFUL, 2:INITIALITION_FAILED, 3:DESTROYED)", _status);
55 return;
56 }
57
58 try {
59 initThreadPool();
60 startThreadPoolStateJob();
61 startThreadStateJob();
62 startThreadStackJob();
63 _status = ThreadPoolStatus.INITIALITION_SUCCESSFUL;
64 } catch (RuntimeException e) {
65 _status = ThreadPoolStatus.INITIALITION_FAILED;
66 throw e;
67 }
68 }
69
70 /**
71 * 初始化所有线程池。
72 */
73 private void initThreadPool() {
74 _threadPoolConfig.init();
75 if (! _threadPoolConfig.containsPool(DEFAULT_THREAD_POOL)) {
76 throw new IllegalStateException( String.format("the default thread pool not exists, please check the config file '%s'", _threadPoolConfig._configFile) );
77 }
78 Collection<ThreadPoolInfo> threadPoolInfoList = _threadPoolConfig.getThreadPoolConfig();
79 for (ThreadPoolInfo threadPoolInfo : threadPoolInfoList) {
80 BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(threadPoolInfo.getQueueSize());
81 ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threadPoolInfo.getCoreSize(), threadPoolInfo.getMaxSize(),
82 threadPoolInfo.getThreadKeepAliveTime(), TimeUnit.SECONDS, workQueue,
83 new DefaultThreadFactory(threadPoolInfo.getName()));
84 _multiThreadPool.put(threadPoolInfo.getName(), threadPool);
85 _logger.info("initialization thread pool '{}' success", threadPoolInfo.getName());
86 }
87 }
88
89 /**

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected