- 论坛徽章:
- 0
|
最近读tomcat源代码,其中线程池部分有一段完全看不懂,ThreadPoolExecutor.java源文件中的一个方法,不知道这个这个方法起什么作用,以及Context reload是什么意思,在什么情况下需要进行Context reload- public void contextStopping() {
- this.lastContextStoppedTime.set(System.currentTimeMillis());
- // save the current pool parameters to restore them later
- int savedCorePoolSize = this.getCorePoolSize();
- TaskQueue taskQueue =
- getQueue() instanceof TaskQueue ? (TaskQueue) getQueue() : null;
- if (taskQueue != null) {
- // note by slaurent : quite oddly threadPoolExecutor.setCorePoolSize
- // checks that queue.remainingCapacity()==0. I did not understand
- // why, but to get the intended effect of waking up idle threads, I
- // temporarily fake this condition.
- taskQueue.setForcedRemainingCapacity(Integer.valueOf(0));
- }
- // setCorePoolSize(0) wakes idle threads
- this.setCorePoolSize(0);
- // wait a little so that idle threads wake and poll the queue again,
- // this time always with a timeout (queue.poll() instead of
- // queue.take())
- // even if we did not wait enough, TaskQueue.take() takes care of timing
- // out, so that we are sure that all threads of the pool are renewed in
- // a limited time, something like
- // (threadKeepAlive + longest request time)
- try {
- Thread.sleep(200L);
- } catch (InterruptedException e) {
- // yes, ignore
- }
- if (taskQueue != null) {
- // ok, restore the state of the queue and pool
- taskQueue.setForcedRemainingCapacity(null);
- }
- this.setCorePoolSize(savedCorePoolSize);
- }
复制代码 |
|