1、想让线程池在初始化时就干活,而不是等到第一次提交任务时才建立线程,该怎么作?缓存
/** * Starts all core threads, causing them to idly wait for work. This * overrides the default policy of starting core threads only when * new tasks are executed. * * @return the number of threads started */ public int prestartAllCoreThreads() { int n = 0; while (addWorker(null, true)) ++n; return n; }
2、tomcat 8 如何优化ThreadPoolExecutortomcat
1. tomcat优化:ThreadPoolExecutor#execute 在抛出 RejectedExecutionException时, 仍然尝试给队列添加任务ide
/** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the <tt>Executor</tt> implementation. * If no threads are available, it will be added to the work queue. * If the work queue is full, the system will wait for the specified * time and it throw a RejectedExecutionException if the queue is still * full after that. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be * accepted for execution - the queue is full * @throws NullPointerException if command or unit is null */ public void execute(Runnable command, long timeout, TimeUnit unit) { submittedCount.incrementAndGet(); try { super.execute(command); } catch (RejectedExecutionException rx) { if (super.getQueue() instanceof TaskQueue) { final TaskQueue queue = (TaskQueue)super.getQueue(); try { if (!queue.force(command, timeout, unit)) { submittedCount.decrementAndGet(); throw new RejectedExecutionException("Queue capacity is full."); } } catch (InterruptedException x) { submittedCount.decrementAndGet(); throw new RejectedExecutionException(x); } } else { submittedCount.decrementAndGet(); throw rx; } } }
2. TaskQueue 继承 LinkedBlockingQueue, 重写了offer方法函数
也能够分析下tomcat线程池在防止内存溢出方面作的工做优化
tomcat的ThreadExecutor其中一个重要的做用,就是对线程的ThreadLocal缓存的变量进行清理;(待验证)this
为何ThreadLocal要进行清理呢?若是是一个简单的main函数的话,那么在这个主线程中使用ThreadLocal缓存在程序结束以后,自动就随着JVM退出而消亡了;spa
若是是开启的一个线程,这个线程中使用了ThreadLocal缓存,线程退出,这种状况这块内存仍旧会进行回收;可是,线程池的线程是重复利用的,线程
颇有可能会在某处使用了ThreadLocal缓存,可是忘记remove掉了,这种在线程池中是很致命的rest