public void stop() { state = State.STOPPING; for (int i = 0; i < works.size(); i++) { msgQueue.offer(this.killThread); } synchronized (works) { for (WorkThread work : works) { work.interrupt(); } } int cnt = 100; while (cnt-- > 0 && works.size() > 0) { Thread.yield(); try { Thread.sleep(50); } catch (Exception e) { } if (cnt < 20) continue; } if (log.isDebugEnabled()) log.debug("Thread Pool【" + name + "】Stop。,Dead thread count=" + works.size()); // 线程池停止,僵死线程数 works.clear(); // 把AsyncService数据回收 for (IMessage msg : this.asycService.revokeEventQueue()) msgQueue.offer(msg); state = State.STOPPED; }
private void justThreadSize() { int n = msgQueue.size(); if (n > 1000) { if (log.isDebugEnabled()) log.debug("adjustment thread pool size(+1)"); forkThreads(1); } else if (n < 2) { if (log.isDebugEnabled()) log.debug("adjustment thread pool size(-1)"); forkThreads(-1); } }
private void forkThreads(int delta) { if (delta == 0) return; if (delta > 0) { // 不能超过最大值 int maxDelta = this.maxSize - works.size(); delta = Math.min(maxDelta, delta); for (; delta > 0; delta--) { new WorkThread(); } } else { // 不能小于1 delta = -delta; int n = works.size() - minSize; // 最多允许减少的线程数 delta = Math.min(delta, n); for (; delta > 0; delta--) { msgQueue.offer(this.killThread); } } }