Chinaunix

标题: 求解tomcat线程池源码 [打印本页]

作者: w1520138768    时间: 2013-07-22 21:05
标题: 求解tomcat线程池源码
最近读tomcat源代码,其中线程池部分有一段完全看不懂,ThreadPoolExecutor.java源文件中的一个方法,不知道这个这个方法起什么作用,以及Context reload是什么意思,在什么情况下需要进行Context reload
  1. public void contextStopping() {
  2.         this.lastContextStoppedTime.set(System.currentTimeMillis());

  3.         // save the current pool parameters to restore them later
  4.         int savedCorePoolSize = this.getCorePoolSize();
  5.         TaskQueue taskQueue =
  6.                 getQueue() instanceof TaskQueue ? (TaskQueue) getQueue() : null;
  7.         if (taskQueue != null) {
  8.             // note by slaurent : quite oddly threadPoolExecutor.setCorePoolSize
  9.             // checks that queue.remainingCapacity()==0. I did not understand
  10.             // why, but to get the intended effect of waking up idle threads, I
  11.             // temporarily fake this condition.
  12.             taskQueue.setForcedRemainingCapacity(Integer.valueOf(0));
  13.         }

  14.         // setCorePoolSize(0) wakes idle threads
  15.         this.setCorePoolSize(0);

  16.         // wait a little so that idle threads wake and poll the queue again,
  17.         // this time always with a timeout (queue.poll() instead of
  18.         // queue.take())
  19.         // even if we did not wait enough, TaskQueue.take() takes care of timing
  20.         // out, so that we are sure that all threads of the pool are renewed in
  21.         // a limited time, something like
  22.         // (threadKeepAlive + longest request time)
  23.         try {
  24.             Thread.sleep(200L);
  25.         } catch (InterruptedException e) {
  26.             // yes, ignore
  27.         }

  28.         if (taskQueue != null) {
  29.             // ok, restore the state of the queue and pool
  30.             taskQueue.setForcedRemainingCapacity(null);
  31.         }
  32.         this.setCorePoolSize(savedCorePoolSize);
  33.     }
复制代码

作者: w1520138768    时间: 2013-07-22 23:31
我自己顶一下




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2