首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java多线程札记6-ThreadPoolExecutor/ExecutorService

2014-01-22 
Java多线程笔记6-ThreadPoolExecutor/ExecutorServicepackage?com.landon.mavs.example.concurrentimport

Java多线程笔记6-ThreadPoolExecutor/ExecutorService
package?com.landon.mavs.example.concurrent;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?java.util.concurrent.LinkedBlockingQueue;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?java.util.concurrent.ThreadPoolExecutor;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?java.util.concurrent.TimeUnit;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?java.util.concurrent.atomic.AtomicInteger;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?org.slf4j.Logger;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?org.slf4j.LoggerFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?com.landon.mavs.example.util.MavsCachedThreadPoolExecutor;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?com.landon.mavs.example.util.MavsFixedThreadPoolExecutor;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?com.landon.mavs.example.util.MavsRejectedExecutionPolicy;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?com.landon.mavs.example.util.MavsThreadFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport?com.landon.mavs.example.util.MavsThreadPoolStateMonitor;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?{@link?java.util.concurrent.ThreadPoolExecutor}示例
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?

Java多线程札记6-ThreadPoolExecutor/ExecutorService
?*?1.public?class?ThreadPoolExecutor?extends?AbstractExecutorServiceJava多线程札记6-ThreadPoolExecutor/ExecutorService?*?Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?2.AbstractExecutorService内部的提交任务方法系列最终均调用了execute方法执行任务.{@link?java.util.concurrent.RunnableFuture}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?3.Executors:Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????//?1.corePoolSize和maximumPoolSize一致.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????//?2.keepAliveTime传入0L,即在队列无元素时则直接不等待直接返回null(因线程池大小不会超过corePoolSize_无界阻塞队列且【1描述】).Java多线程札记6-ThreadPoolExecutor/ExecutorService?*????????????????因该参数是在getTask方法当(poolSize?>?corePoolSize?||?allowCoreThreadTimeOut)时调用,而第一个条件已经是false了,即当队列空的时候Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????则一直会阻塞(take).所以只有在线程池设置了allowCoreThreadTimeOut参数时才会进行调用.而在allowCoreThreadTimeOut(boolean?value)方法Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????的实现中,如果(value?&&?keepAliveTime?<=?0)则抛出异常.即allowCoreThreadTimeOut(true)和keepAliveTime<=0这两个参数不能同时存在.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????所以在FixedThreadPool实现中keepAliveTime参数无效(即永远不会回收Worker线程).Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????//?3.workQueue为LinkedBlockingQueue(未指定capacity),即无界阻塞队列.则线程池大小>=corePoolSize时则将任务插入队列.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????//?4.总结:FixedThreadPool正如其名字一样,线程池中的线程数目是Fixed,固定的,Worker线程不会被回收且队列无任务时则一直阻塞.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????public?static?ExecutorService?newFixedThreadPool(int?nThreads)?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????????return?new?ThreadPoolExecutor(nThreads,?nThreads,Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????????????????????????????????0L,?TimeUnit.MILLISECONDS,Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????????????????????????????????new?LinkedBlockingQueue());Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????//?单线程+无界阻塞消息队列的经典模型.无线程安全问题.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?注意其返回的是封装的FinalizableDelegatedExecutorService并实现了finalize方法,而finalize方法则调用了线程池的shutdown方法.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?同时要主要强转的问题.其实际类型不是ThreadPoolExecutorJava多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?而FinalizableDelegatedExecutorService继承了DelegatedExecutorService(委托?/代理),其只是包装了,仅暴露了ExecutorService的实现方法.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?个人认为因为其就是是单线程的,所以完全没有必要暴露ThreadPoolExecutor的所有访问方法,暴露了反而可能因为不必要的麻烦.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?但是之所以再封装一层finalize就不知为何了.(GC回收之前的调用?有啥必要呢?如果我们没有对它调用shutdown(),那么可以确保它在被回收时调用shutdown()来终止线程)Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?所以只能用安全网来解释这个设计了Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??//?landon:终于明白了.1.其主要原因只是要暴露ExecutorService的方法,不要暴露ThreadPoolExecutor的所有访问方法Java多线程札记6-ThreadPoolExecutor/ExecutorService?*????????????????????2.加上finalize的原因在于ThreadPoolExecutor本身有finalize方法,且实现为shutdown.而DelegatedExecutorService本身是没有的.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????所以额外加了在FinalizableDelegatedExecutorService加上了finalize.与ThreadPoolExecutor的finalize保持一致.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????public?static?ExecutorService?newSingleThreadExecutor()?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????return?new?FinalizableDelegatedExecutorServiceJava多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????(new?ThreadPoolExecutor(1,?1,Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????????????????????????0L,?TimeUnit.MILLISECONDS,Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????????????????????????new?LinkedBlockingQueue()));Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???//?1.corePoolSize为0,maximumPoolSize为Integer.MAX_VALUE.则执行任务的时候会直接向workQueue.offer任务.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???//?2.workQueue为SynchronousQueue,即同步阻塞队列(非公平),即offer的是恰好有线程poll才可以成功.第一次执行任务的时候,offer肯定fail.所以Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????->addIfUnderMaximumPoolSize->即添加一个worker线程.(常规情况下会一直UnderMaximum.因为Integer.MAX_VALUE)Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???//?3.keepAliveTime为60秒.而poolSize一定大于corePoolSize(为0)->workQueue.poll(keepAliveTime)->即从工作队列poll.所以说Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????如果在60秒内有任务offer则worker线程getTask成功则执行任务;否则返回null,又因为是同步阻塞队列所有在判断workerCanExit的时候(isEmpty永远为true),Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????所以worker线程会退出被回收.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???//?总结:1.如果线程池繁忙的情况下,每个线程都在执行任务的时候,新任务会新建新的worker线程去执行任务.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????2.假如在提交新任务的时候恰好有线程正在空闲getTask(60s超时内)则会委托空闲线程去做.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????3.如果线程池不繁忙,偶尔来一个任务.则第一个任务会创建一个workder线程,此时执行完毕如果1分钟内还没有任务则该线程会被自动回收.即该线程池最小的线程数目其实是0.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???public?static?ExecutorService?newCachedThreadPool()?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????return?new?ThreadPoolExecutor(0,?Integer.MAX_VALUE,Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????????????????????????????????60L,?TimeUnit.SECONDS,Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????????????????????????????????new?SynchronousQueue());Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?4.RejectPolicyJava多线程札记6-ThreadPoolExecutor/ExecutorService?*?????1.ThreadPoolExecutor内部预定义了4中拒绝的处理程序策略Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??2.Reject的执行时机:Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????1.当提交一个任务t的时候,线程池中数目超出了coreSize->提交至workQueue.此时检查线程池状态不在是running或线程池中突然没有了线程(有可能是额外线程调用了线程池Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????的shutdown/shutdownNow)->ensureQueuedTaskHandled->即如果发现此时状态不是running且可以从workQueue将t移除,则执行拒绝策略.即shutdown的时候Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????新的任务会被拒绝.->后续如果发现是调用了shutdown(线程池状态是SHUTDOWN)->且workQueue不为空且poolSize添加worker线程->即保证Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????队列任务执行完毕.{@link?#shutdown}方法会中断空闲的线程(超出coreSize的线程)?{@link?getTask}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????2.当线程池线程数目超出maximumPoolSize的时候则执行拒绝策略.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????3.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????1.AbortPolicy:终止策略Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????//?其是线程池的默认拒绝策略defaultHandler.->execute方法的调用线程则直接抛出异常Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????public?void?rejectedExecution(Runnable?r,?ThreadPoolExecutor?e)?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????throw?new?RejectedExecutionException();Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????2.CallerRunsPolicy:调用者运行策略Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????//?直接在execute方法的调用线程运行被拒绝的任务.如果线程池已关闭则丢弃该任务.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????//?因为是在execute的调用线程中运行的.所以可简单的减缓新任务的提交速度.即得等到执行完被线程池拒绝的任务后才能提交任务.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????public?void?rejectedExecution(Runnable?r,?ThreadPoolExecutor?e)?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????if?(!e.isShutdown())?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????r.run();Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????3.DiscardOldestPolicy:丢弃最旧的任务策略Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????//?放弃最旧的未处理的任务(即队头元素),重新提交执行被拒绝的任务r.如果线程池已关闭则丢弃该任务.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????public?void?rejectedExecution(Runnable?r,?ThreadPoolExecutor?e)?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????if?(!e.isShutdown())?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????e.getQueue().poll();Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????????e.execute(r);Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????4.DiscardPolicy:丢弃策略Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????//?空实现,即直接丢弃被拒绝的任务Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????public?void?rejectedExecution(Runnable?r,?ThreadPoolExecutor?e)?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???5.钩子方法:Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????1.protected?void?beforeExecute(Thread?t,?Runnable?r),可在子类覆写.在执行的线程运行任务之前调用的方法.此方法由t调用.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???????????->(方法结束时,子类通常应该调用super.beforeExecute->嵌套多个重写操作)Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????2.protected?void?afterExecute(Runnable?r,?Throwable?t)?,可在子类覆写,完成给定任务后所调用的方法.此方法由执行任务的worker线程Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????调用.t为执行该任务时导致终止时的异常->该异常会被抛到上层run然后被try.(没有catch).->如果t为null,则表示任务执行顺利.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????注:当提交的任务类似于submit方法提交的(如FutureTask时)->{@link?FutureTask$Sync#innerRun}会在内部捕获该异常.所以其不会导致workerJava多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????线程突然终止.而异常也不会传递给该方法。Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????->(方法开始时,子类通常应该调用super.afterExecute->嵌套多个重写操作)Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????注:workerDone方法是在worker线程结束时调用的方法->完成任务计数Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????3.protected?void?terminated,此为线程池终止时调用的方法{@link?#tryTerminate}.子类可覆写.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??6.public?BlockingQueue?getQueue(),该方法用来访问工作队列.->用于监控和调试目的.->强烈反对出于其他目的而使用此方法.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??7.public?boolean?remove(Runnable?task),从线程池的内部工作队列中移除此任务.如果其尚未开始,则其不再运行.注:对于通过submit输入的runnable无法移除.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????因为其已经被转换了其他形式如FutureTask.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??8.?public?void?purge(),尝试从工作队列移除已取消的Future任务.->取消的任务不会再次执行.但是他们可能在工作队列中累积.直到worker线程将其主动移除(从工作队列poll).Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????该方法则试图移除他们.如果出现其他线程的干预->则抛出ConcurrentModificationException.则失败.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??9.?即使用户忘记调用了shutdown关闭线程池:也希望确保可回收线程->设置keepAliveTime/allowCoreThreadTimeOut/corePoolSize为0.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????{@link?#getTask()}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??10.public?boolean?prestartCoreThread()?启动核心线程,使其处于getTask的空闲状态.?如果已启动了线程,则返回trueJava多线程札记6-ThreadPoolExecutor/ExecutorService?*??????从源码上:其内部直接调用了addIfUnderCorePoolSize(null).既如果coreSize不为0,则会启动一个worker线程并处于getTask的等待状态Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??11.public?int?prestartAllCoreThreads()?启动所有核心线程,使其处于等待任务的空闲状态Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????从源码上:?while?(addIfUnderCorePoolSize(null))->即超出coreSize则跳出循环.?返回已启动的线程数Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??12.Worker#isActiveJava多线程札记6-ThreadPoolExecutor/ExecutorService?*??????//?runLock是在runTask方法内的锁.而不是run的锁.即如果线程在getTask等待空闲的时候不是active.只有在真正执行任务的时候是active.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????boolean?isActive()?{Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????????return?runLock.isLocked();Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?????????}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*???Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??13.public?void?setCorePoolSize(int?corePoolSize)?设置核心线程数Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????从源码上:1.设置coreSize为传入的新值Java多线程札记6-ThreadPoolExecutor/ExecutorService?*????????????2.如果新值大于旧值,则会添加额外线程,但是启动的线程数目一定不会超过当前工作队列的大小.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*????????????3.如果新值小于旧值,则会遍历当前所有的worker线程,将多出的线程进行interruptIfIdle().其中还有一个条件是workQueue.remainingCapacity()?==?0.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????????也就是说要求此时工作队列的可附加元素数量为0,则当前工作队列已满.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????????(个人认为这个条件的添加是因为如果此时工作队列已满,则再次提交任务的时候会在maximum之下继续添加线程的.也就是说在这时候中断一个core线程是没有问题的.)Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????????->如果条件不满足的时候则多余的现有线程将在下一次空闲时终止(因为poolSize?>?coreSize).{@link?#getTask}Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??14.public?void?setMaximumPoolSize(int?maximumPoolSize)?设置运行最大的线程数Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????从源码上:1.参数maximumPoolSize必须>0?且?>=corePoolSize,否则抛出IllegalArgumentException.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*????????????2.设置新值.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*????????????3.如果新值小于当前值且当前poolSize?>?maximumPoolSize->则会遍历工作线程,将多余的线程interruptIfIdle.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??15.线程池在调用了shutdown方法后便不能在提交任务了,因为此时的线程池状态已经不是running了.但是如果线程因为执行任务而异常终止的话,却依然可以提交任务.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*??????因为此时状态还是running.Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?

Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?@author?landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublic?class?ThreadPoolExecutorExample?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????private?static?final?Logger?LOGGER?=?LoggerFactory
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????.getLogger(ThreadPoolExecutorExample.class);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????public?static?void?main(String[]?args)?throws?Exception?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?固定2个线程的线程池
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????MavsFixedThreadPoolExecutor?fixedThreadPoolExecutor1?=?new?MavsFixedThreadPoolExecutor(
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????2,?new?MavsThreadFactory("Example",?"FixedThreadPool-1"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从线程池的状态监视器来看:此时poolSize=1/workQueueSize=0,即启动了一个线程,工作队列没有任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从线程池的状态监视器来看:此时poolSize=2/workQueueSize=0,即又启动了一个线程
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?由提交了3个任务,从输出来看:poolSize一直为2.而workQueueSize最多为3->随着任务的执行,workQueueSize变为0
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?所以MavsFixedThreadPoolExecutor这个线程池会保持固定线程数量
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?执行shutdown
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?另外从输出看:发现线程池终止的时候调用了terminate方法
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?测试shutdown后,还可以执行任务吗?
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?答案当然是NO.因为新建worker线程的条件包括插入队列都必须是在RUNNING状态下.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?而执行了shutdown后则更改了运行状态为SHUTDOWN
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?cached线程池
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????MavsCachedThreadPoolExecutor?cachedThreadPoolExecutor1?=?new?MavsCachedThreadPoolExecutor(
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsThreadFactory("Example",?"CachedThreadPool-1"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以看到,线程池最多启动了5个线程,workQueueSize一直为0
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????cachedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????cachedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????cachedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????cachedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????cachedThreadPoolExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?暂停2分钟,使得默认空闲1分钟的worker线程退出
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(2?*?60?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以看到:poolSize=0,即空闲的worker线程被回收了.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?另外所有的worker线程被回收了,线程池就结束了.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?因为:ThreadPoolExecutor#void?workerDone(Worker?w)->
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?if?(--poolSize?==0)tryTerminate()
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?但是这种线程自然结束的话,并没有调用覆写的terminate方法.因为tryTerminate的实现中是判断当前线程池状态是STOP/SHUTDOWN的时候才执行terminated方法的
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("cachedThreadPoolExecutor1.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(cachedThreadPoolExecutor1));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?单线程线程池,注意这个和{@link
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?Executors#newSingleThreadExecutor的区别},后者仅是返回的暴露的ExecutorService接口
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????MavsFixedThreadPoolExecutor?singleExecutor?=?new?MavsFixedThreadPoolExecutor(
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????1,?new?MavsThreadFactory("Example",?"SingleThreadPool-1"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?提交一个可抛出异常的任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出看出
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?1:执行了afterExecute方法且其中的Throwable?t为不null.此执行任务的时候抛出了异常.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?2.线程因为异常终止,因指定了线程默认的UncaughtExceptionHandler,所以执行了uncaughtException方法.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????singleExecutor.execute(new?ThreadPoolExceptionTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以看到:poolSize=0变为了0.即线程终止了.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?因为Worker线程的run方法只是try/finally,即并没有捕获异常.而runTask向上抛出异常至run,直接到finally.->workerDone->poolSize--
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?->tryTerminate
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("singleExecutor.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(singleExecutor));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?测试线程池异常终止后,还可以执行任务吗?
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?答案是YES.因为此时的线程池状态依然是RUNNING.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????singleExecutor.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出发现:poolSize=1,即新增了一个worker线程.另外从线程的名字Mavs-Example-SingleThreadPool-1-2也可看得出.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("singleExecutor.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(singleExecutor));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?这里是提交了一个任务,内部会被封装成->RunnableFuture->FutureTask
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?而其内部run->Sync#innerRun->其内部会被try/catch的->所以理论上结果应该线程应该不会异常终止.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出看:1.afterExecute方法中的异常参数为null.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?2.没用调用默认的UncaughtExceptionHandler.也就是说线程正常运行.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????singleExecutor.submit(new?ThreadPoolExceptionTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????singleExecutor.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?测试setCoreSize以及setMaximumSize
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?3个固定线程数目的线程池
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????MavsFixedThreadPoolExecutor?fixedThreadPoolExecutor2?=?new?MavsFixedThreadPoolExecutor(
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????3,?new?MavsThreadFactory("Example",?"FixedThreadPool-2"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?设置核心线程大小为6.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.setCorePoolSize(6);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出看:poolSize=6
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor2.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor2));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?设置核心线程大小为2
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.setCorePoolSize(2);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出看.poolSize=6
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?因为?workQueue.remainingCapacity()此时不为0,即不会中断多余的空闲线程.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?另外此时所有的worker线程正在处在等待状态.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor2.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor2));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以看到:此时poolSize=5.因为某个等待线程获得执行机会后再次getTask后->会执行pool(keepAliveTime),则直接回收退出.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor2.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor2));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?继续执行3个任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以发现:此时poolSize=2,因为多余的线程在执行完任务下次getTask判断的时候直接就被回收了.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?另外:此时maximumSize是3.coreSize为2.也就是说此时的线程池已经不再是固定数量线程的线程池了.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor2.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor2));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor2.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?测试setMaximumPoolSize
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?2个固定线程数目的线程池
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????MavsFixedThreadPoolExecutor?fixedThreadPoolExecutor3?=?new?MavsFixedThreadPoolExecutor(
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????2,?new?MavsThreadFactory("Example",?"FixedThreadPool-3"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?设置最大线程池大小为4
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.setMaximumPoolSize(4);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?提交一系列任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出看:maximumPoolSize=4/poolSize=2
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?即只是修改了maximumPoolSize的值/poolSize仍然为2.因为用的是无限阻塞队列,所以多余的任务都被放到了队列.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor3.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor3));
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????try?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????//?这里抛出了一个异常,因为1比coreSize?2还要小
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????fixedThreadPoolExecutor3.setMaximumPoolSize(1);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}?catch?(Exception?e)?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????LOGGER.warn("fixedThreadPoolExecutor3.setMaximumPoolSize.err.",?e);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor3.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?自定义线程池1
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?工作队列为容量3的阻塞队列
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?等待空闲时间为60s
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????ThreadPoolExecutor?userDefinedExecutor1?=?new?ThreadPoolExecutor(2,?4,
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????10,?TimeUnit.SECONDS,?new?LinkedBlockingQueue<Runnable>(3),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsThreadFactory("Example",?"User-Define-Executor-1"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?直接提交很多任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?这个测试的目的在于测试拒绝策略.从输出可以看到:
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?poolSize=4/workQueueSize=3这个时候,即已经达到了最大线程数目和队列上限,则执行了拒绝策略.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????for?(int?i?=?0;?i?<?20;?i++)?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????userDefinedExecutor1.execute(new?ThreadPoolTask());
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(5?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("userDefinedExecutor1.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(userDefinedExecutor1));
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?将线程池最大池数目调整为3.此时的poolSize为4.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????userDefinedExecutor1.setMaximumPoolSize(3);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(1?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出看:poolSize还是为4.因为此时所有的worker线程都在poll(timeout)->然后setMaximumPoolSize->会中断一个空闲线程->但是getTask这里
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?被try/catch了.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?不过多余的线程在空闲的时候都会被回收.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("userDefinedExecutor1.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(userDefinedExecutor1));
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????Thread.sleep(5?*?1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("userDefinedExecutor1.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(userDefinedExecutor1));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????userDefinedExecutor1.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?测试prestartCoreThread()/prestartAllCoreThreads
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????MavsFixedThreadPoolExecutor?fixedThreadPoolExecutor4?=?new?MavsFixedThreadPoolExecutor(
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????3,?new?MavsThreadFactory("Example",?"FixedThreadPool-4"),
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????new?MavsRejectedExecutionPolicy());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor4.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor4));
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?启动一个核心线程
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor4.prestartCoreThread();
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以看出:poolSize为1,即启动了一个worker.
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor4.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor4));
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?启动所有核心线程
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????//?从输出可以看出:poolSize为3,即现在启动了所有的核心线程
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????fixedThreadPoolExecutor4.prestartAllCoreThreads();
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????LOGGER.debug("fixedThreadPoolExecutor4.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????MavsThreadPoolStateMonitor.monitor(fixedThreadPoolExecutor4));
Java多线程札记6-ThreadPoolExecutor/ExecutorService????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?用于测试的线程池任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?@author?landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*/
Java多线程札记6-ThreadPoolExecutor/ExecutorService????private?static?class?ThreadPoolTask?implements?Runnable?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????private?static?final?AtomicInteger?COUNTER?=?new?AtomicInteger(1);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????private?int?id;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????public?ThreadPoolTask()?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????id?=?COUNTER.getAndIncrement();
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????@Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????public?void?run()?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????LOGGER.debug(this?+?"?begin");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????try?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????TimeUnit.MICROSECONDS.sleep(100);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????}?catch?(InterruptedException?e)?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????LOGGER.warn(this?+?"?was?interrupted",?e);
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????LOGGER.debug(this?+?"?end");
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????@Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????public?String?toString()?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????return?"ThreadPoolTask?[id="?+?id?+?"]"?+?"["
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????????+?Thread.currentThread().getName()?+?"]";
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?用于测试的线程池异常任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?@author?landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*?
Java多线程札记6-ThreadPoolExecutor/ExecutorService?????*/
Java多线程札记6-ThreadPoolExecutor/ExecutorService????private?static?class?ThreadPoolExceptionTask?implements?Runnable?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????private?static?final?AtomicInteger?COUNTER?=?new?AtomicInteger(1);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????private?int?id;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????public?ThreadPoolExceptionTask()?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????id?=?COUNTER.getAndIncrement();
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????@Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????public?void?run()?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????LOGGER.debug(this?+?"?begin");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????throw?new?RuntimeException("ThreadPoolExceptionTask.Exception.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????@Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????public?String?toString()?{
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????return?"ThreadPoolExceptionTask?[id="?+?id?+?"]"?+?"["
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????????????????+?Thread.currentThread().getName()?+?"]";
Java多线程札记6-ThreadPoolExecutor/ExecutorService????????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService????}
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



2.ExecutorService示例代码


Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.concurrent;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.ArrayList;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.Arrays;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.Iterator;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.List;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.Callable;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ExecutionException;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ExecutorService;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.Executors;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.Future;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.TimeUnit;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.TimeoutException;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.Logger;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.LoggerFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * ExecutorServiceExample
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass ExecutorServiceExample {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticfinal Logger LOGGER = LoggerFactory
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? .getLogger(ExecutorServiceExample.class);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? publicstaticvoid main(String[] args) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? ExecutorService exeSrv = Executors.newFixedThreadPool(4);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // execute(Runnable command) 执行一个Runnable
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv.execute(new OneRunnable(1));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // Future submit(Runnable task) 提交一个Runable
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future oneRunFuture = exeSrv.submit(new OneRunnable(2));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // Future#isDone 返回任务是否结束
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneRun is complete:"+ oneRunFuture.isDone());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 等待计算完成,返回计算结果
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 当前成功完成的时候 #get 返回null
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneRun result:"+ oneRunFuture.get());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneRun#get is interrupted while waiting result.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (ExecutionException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneRun#get compuation throws a exception");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // Future submit(Callable task) 提交一个Callable
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future<String> oneCallFuture = exeSrv.submit(new OneCallable(1));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // V get() throws InterruptedException, ExecutionException
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 等待计算完成,返回计算结果
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneCall result:"+ oneCallFuture.get());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneCall#get is interrupted while waiting result.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (ExecutionException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneCall#get compuation throws a exception");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future<String> oneCallFuture2 = exeSrv.submit(new OneCallable(2));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // V get(long timeout, TimeUnit unit) 指定等待超时时间
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneCall2 result:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? + oneCallFuture2.get(1, TimeUnit.SECONDS));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneCall2#get is interrupted while waiting result.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (ExecutionException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneCall2#get compuation throws a exception");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (TimeoutException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneCall2#get timeout");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future<String> oneCallFuture3 = exeSrv.submit(new OneCallable(3));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // boolean cancel(boolean mayInterruptIfRunning)
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 尝试取消任务的执行.如果任务已完成或者已经被取消或者因为其他一些原因被能取消则尝试会失败
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 如果尝试成功且任务还未开始则该任务再也不会运行.如果任务已经启动,mayInterruptIfRunning参数决定任务执行线程是否中断尝试结束任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? boolean isFuture3CancelSuccess = oneCallFuture3.cancel(false);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture3#cancel(false) result:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + isFuture3CancelSuccess);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture3#isDone:"+ oneCallFuture3.isDone());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture3#isCancelled:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + oneCallFuture3.isCancelled());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future<String> oneCallFuture4 = exeSrv.submit(new OneCallable(4));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 主线程暂停2秒后执行cancel
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? TimeUnit.SECONDS.sleep(2);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 此处cancel传true则表明如果任务已启动则中断执行任务线程尝试结束任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 从输出可以看到,即输出了任务开始,但是却没有输出任务结束->且返回true表明任务被中断取消
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? boolean isFuture4CancelSuccess = oneCallFuture4.cancel(true);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture4#cancel(true) result:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + isFuture4CancelSuccess);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture4#isDone:"+ oneCallFuture4.isDone());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture4#isCancelled:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + oneCallFuture4.isCancelled());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future<String> oneCallFuture5 = exeSrv.submit(new OneCallable(5));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 主线程暂停8秒后执行cancel,此时任务有可能已经执行完毕
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? TimeUnit.SECONDS.sleep(8);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 从输入可以看到,任务5输出了end.即cancel时任务已经完成.所以isFuture5CancelSuccess为false.isDone为true.isCancelled为false
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? boolean isFuture5CancelSuccess = oneCallFuture5.cancel(true);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture5#cancel(true) result:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + isFuture5CancelSuccess);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture5#isDone:"+ oneCallFuture5.isDone());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("oneCallFuture5#isCancelled:"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + oneCallFuture5.isCancelled());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // Future submit(Runnable task, T result) 当任务完成时get方法会返回指定的result
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Future<String> oneRun3Future = exeSrv
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? .submit(new OneRunnable(3), "isOk");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 从输入可以看到get方法的返回是传入的"isOk"
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneRun3 result:"+ oneRun3Future.get());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneRun3l#get is interrupted while waiting result.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (ExecutionException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exception_oneRun3#get compuation throws a exception");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 任务集合
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? List<OneCallable> oneCallList = Arrays.asList(new OneCallable(10),
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new OneCallable(11), new OneCallable(12));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // List> invokeAll(Collection>
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // tasks) throws InterruptedException;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 相当于批量执行任务.从方法的异常列表可以看出.此方法会等待(即阻塞)直到所有任务完成
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? List<Future<String>> oneCallListFutures = exeSrv
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? .invokeAll(oneCallList);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 处理完成结果 从输出可以看到->invokeAll确实是在等待所有任务执行完毕.
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? List<Boolean> resultList =new ArrayList<Boolean>();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? for (Iterator<Future<String>> iterator = oneCallListFutures
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? .iterator(); iterator.hasNext();) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? if (iterator.next().isDone()) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? resultList.add(true);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneCallListFutures result: "+ resultList);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exeSrv#invokeAll(oneCallList) exception_waiting all task complete was interrupted.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 任务集合2
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? List<OneCallable> oneCallList2 = Arrays.asList(new OneCallable(20),
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new OneCallable(21), new OneCallable(22));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // T invokeAny(Collection> tasks) throws
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // InterruptedException, ExecutionException;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 批量执行任务->等待直到某个任务已成功完成(注意只要某个任务成功返回则返回结果) 另外注意返回结果是T,而非Future
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? String oneCallList2Result = exeSrv.invokeAny(oneCallList2);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 从输出结果可以看到:
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // [oneCallList2Result:OneCallable [taskNum=20]OK],即20号任务执行完成即返回了
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneCallList2Result:"+ oneCallList2Result);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exeSrv#invokeAny(oneCallList2) exception_waiting one task complete was interrupted.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (ExecutionException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exeSrv#invokeAll(oneCallList2) exception_one any one task was completed.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 任务集合3
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? List<OneCallable> oneCallList3 = Arrays.asList(new OneCallable(30),
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new OneCallable(31), new OneCallable(32));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // List> invokeAll(Collection>
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // tasks, long timeout, TimeUnit unit) throws InterruptedException;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 批量执行任务->指定等待超时时间->注意该方法并不会抛出超时异常,即如果没有被打断的情况下,超时后(直接返回),则某些任务只是未完成而已(注返回后会取消尚未完成的任务)
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? List<Future<String>> oneCallList3Futures = exeSrv.invokeAll(
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? oneCallList3, 2, TimeUnit.SECONDS);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? List<Boolean> oneCallList3Results =new ArrayList<Boolean>();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? for (Future<String> future : oneCallList3Futures) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? if (future.isDone()) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? oneCallList3Results.add(true);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? }else{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????????? oneCallList3Results.add(false);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 从输出看,这个方法很特殊.即返回的Future列表的isDone方法都返回true.且所有的任务没有输出结束end.
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 从API看,即当所有任务完成或者超时(无论哪个首先发生)则返回的Future列表的isDone方法返回true
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 一旦返回后,即取消尚未完成的任务
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("oneCallList3Results:"+ oneCallList3Results);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("exeSrv#invokeAll(oneCallList3, 2, TimeUnit.SECONDS) exception_waiting all task compelte was interrupted.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 任务集合4
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? List<OneCallable> oneCallList4 = Arrays.asList(new OneCallable(40),
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new OneCallable(41), new OneCallable(42));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // T invokeAny(Collection> tasks,long
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // timeout, TimeUnit unit) throws InterruptedException,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // ExecutionException, TimeoutException;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 批量执行任务->指定等待超时时间->注意这个方法抛出了TimeoutException->即在等待超时后会抛出异常
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 从输出结果可以看到,在等待超时后->尚未完成的任务都被取消了,因为输出只有begin没有end
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? exeSrv.invokeAny(oneCallList4, 1, TimeUnit.SECONDS);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("exeSrv#invokeAny(oneCallList4, 1, TimeUnit.SECONDS) exception_waiting any one task complete was interrupted.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (ExecutionException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("exeSrv#invokeAny(oneCallList4, 1, TimeUnit.SECONDS) exception_no any one task was completed");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (TimeoutException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("exeSrv#invokeAny(oneCallList4, 1, TimeUnit.SECONDS) exception_waiting timeout");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // void shutdown() 启动一次顺序关闭,执行以前提交的任务,但是不接受新任务.如果已经关闭,则调用没有其他作用
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // boolean isShutdown()
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // ThreadPoolExecutor#isShutdown{return runState != RUNNING}
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("exeSrv#shutdown.isShutdown:"+ exeSrv.isShutdown());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // isTerminated
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // ThreadPoolExecutor#isTerminated{return runState == TERMINATED}
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 如果关闭后所有任务都完成,则返回true.注:必须要先调用shutdown/shutdownNow
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 该方法可结合awaitTermination使用awaitTermination,即if(!isTerminated){awaitTermination}
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("exeSrv#shutdown.isTerminated:"+ exeSrv.isTerminated());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? ExecutorService exeSrv2 = Executors.newFixedThreadPool(2);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv2.submit(new OneRunnable(50));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv2.submit(new OneCallable(60));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // List shutdownNow()
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 试图终止所有正在执行的活动任务.暂停处理正在等待的任务,并返回等待执行的任务列表
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 无法保证能够停止正在处理的活动执行任务,但是会尽力尝试.如通过Thread.interrupt这种典型的实现来取消->所以任何任务无法响应中断都可能永远无法停止
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 从输出可以看到50号任务被interrupt了(异常被捕获了).而60号的任务其实也被interrupt了,但是异常被抛出到了上层.
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv2.shutdownNow();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("exeSrv2#shutdown.isShutdown:"+ exeSrv2.isShutdown());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("exeSrv2#shutdown.isTerminated:"+ exeSrv2.isTerminated());
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? ExecutorService exeSrv3 = Executors.newFixedThreadPool(2);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv3.submit(new OneRunnable(70));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv3.submit(new OneRunnable(80));
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? exeSrv3.shutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // boolean awaitTermination(long timeout, TimeUnit unit) throws
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // InterruptedException
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 1.阻塞直到shutdown请求后,所有任务完成 2.阻塞直到超时 3.阻塞直到当前线程被中断
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? if (!exeSrv3.isTerminated()) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? exeSrv3.awaitTermination(10, TimeUnit.SECONDS);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? // 从输出看.任务花费了5秒即执行完毕(多线程并行).所以线程池所有任务任务完成后,awaitTermination也不在阻塞.
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? LOGGER.debug("exeSrv3EwaitTermination(10, TimeUnit.SECONDS) end.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug("exeSrv3.awaitTermination(10, TimeUnit.SECONDS) was interrupted.");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticclass OneRunnable implements Runnable {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? privateint taskNum;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? public OneRunnable(int taskNum) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? this.taskNum = taskNum;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? publicvoid run() {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug(this+" beginJava多线程札记6-ThreadPoolExecutor/ExecutorService");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 用sleep模拟业务逻辑耗时
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? try{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? TimeUnit.SECONDS.sleep(5);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? }catch (InterruptedException e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? LOGGER.warn("execute"+this+" was interruptJava多线程札记6-ThreadPoolExecutor/ExecutorService");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug(this+" endJava多线程札记6-ThreadPoolExecutor/ExecutorService");
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? public String toString() {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? return"OneRunnable [taskNum="+ taskNum +"]";
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticclass OneCallable implements Callable<String>{
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? privateint taskNum;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? public OneCallable(int taskNum) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? this.taskNum = taskNum;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? public String call() throws Exception {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug(this+" begin");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? // 用sleep模拟业务逻辑耗时
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? Thread.sleep(3*1000);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.debug(this+" end");
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? returnthis+" OK";
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? public String toString() {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? return"OneCallable [taskNum="+ taskNum +"]";
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



3.附:MavsCachedThreadPoolExecutor/MavsFixedThreadPoolExecutor/MavsRejectedExecutionPolicy/MavsThreadDefaultUncaughtExceptionHandler/
MavsThreadFactory/MavsThreadPoolExecutor/MavsThreadPoolStateMonitor源码

Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.RejectedExecutionHandler;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.SynchronousQueue;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.TimeUnit;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * Mavs Cache线程池 {@link java.util.concurrent.Executors#newCachedThreadPool()}
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsCachedThreadPoolExecutor extends MavsThreadPoolExecutor {
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsCachedThreadPoolExecutor() {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new SynchronousQueue<Runnable>());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsCachedThreadPoolExecutor(ThreadFactory threadFactory) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new SynchronousQueue<Runnable>(), threadFactory);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsCachedThreadPoolExecutor(ThreadFactory threadFactory,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? RejectedExecutionHandler rejectHandler) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new SynchronousQueue<Runnable>(), threadFactory, rejectHandler);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.LinkedBlockingQueue;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.RejectedExecutionHandler;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.TimeUnit;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * 封装的固定线程数量的Mavs线程池
Java多线程札记6-ThreadPoolExecutor/ExecutorService * {@link java.util.concurrent.Executors#newFixedThreadPool(int)}
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsFixedThreadPoolExecutor extends MavsThreadPoolExecutor {
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsFixedThreadPoolExecutor(int nThreads) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new LinkedBlockingQueue<Runnable>());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsFixedThreadPoolExecutor(int nThreads, ThreadFactory threadFactory) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new LinkedBlockingQueue<Runnable>(), threadFactory);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsFixedThreadPoolExecutor(int nThreads,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? ThreadFactory threadFactory, RejectedExecutionHandler rejectHandler) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? new LinkedBlockingQueue<Runnable>(), threadFactory,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? rejectHandler);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.RejectedExecutionHandler;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadPoolExecutor;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.Logger;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.LoggerFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * Mavs线程池拒绝执行策略
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsRejectedExecutionPolicy implements RejectedExecutionHandler {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticfinal Logger LOGGER = LoggerFactory
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? .getLogger(MavsRejectedExecutionPolicy.class);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? publicvoid rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.debug("task rejectedExecution.ThreadPool.state:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? MavsThreadPoolStateMonitor.monitor(executor));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.lang.Thread.UncaughtExceptionHandler;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.Logger;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.LoggerFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * Mavs线程默认的异常终止处理器
Java多线程札记6-ThreadPoolExecutor/ExecutorService * {@link java.lang.ThreadGroup#uncaughtException(Thread, Throwable)}
Java多线程札记6-ThreadPoolExecutor/ExecutorService * 中对于ThreadDeath的处理
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsThreadDefaultUncaughtExceptionHandler implements
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? UncaughtExceptionHandler {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticfinal Logger LOGGER = LoggerFactory
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? .getLogger(MavsThreadDefaultUncaughtExceptionHandler.class);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? publicvoid uncaughtException(Thread t, Throwable e) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.warn("Exception in thread "" + t.getName() + """, e);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.atomic.AtomicInteger;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * Mavs线程工厂 {@link java.util.concurrent.Executors#defaultThreadFactory()} 参考
Java多线程札记6-ThreadPoolExecutor/ExecutorService * {@link java.util.concurrent.Executors$DefaultThreadFactory}实现
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsThreadFactory implements ThreadFactory {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticfinal String MAVS_NAME_PREFIX ="Mavs-";
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? /** 线程号 */
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatefinal AtomicInteger threadNumber =new AtomicInteger(1);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? /** 线程组 */
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatefinal ThreadGroup threadGroup;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? /** 线程名字前缀 */
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatefinal String namePrefix;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? /**
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? *
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? * 构造MavsThreadFactory
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? *
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? * @param processPrefix
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? *??????????? 进程前缀
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? * @param threadName
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? *??????????? 线程名
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? */
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsThreadFactory(String processPrefix, String threadName) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? SecurityManager sm = System.getSecurityManager();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? threadGroup = (sm !=null) ? sm.getThreadGroup() : Thread
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? .currentThread().getThreadGroup();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? namePrefix = MAVS_NAME_PREFIX + processPrefix +"-"+ threadName +"-";
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public Thread newThread(Runnable r) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? Thread t =new Thread(threadGroup, r, namePrefix
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + threadNumber.getAndIncrement(), 0);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 做这两个设置的原因在于线程的daemon/priority属性默认是由Thread.currentThread决定
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? if (t.isDaemon()) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? t.setDaemon(false);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? if (t.getPriority() != Thread.NORM_PRIORITY) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? t.setPriority(Thread.NORM_PRIORITY);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 设置Mavs线程默认的异常终止处理器
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? if (Thread.getDefaultUncaughtExceptionHandler() ==null) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? Thread.setDefaultUncaughtExceptionHandler(new MavsThreadDefaultUncaughtExceptionHandler());
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? return t;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public String getNamePrefix() {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? return namePrefix;
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService




Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.BlockingQueue;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.RejectedExecutionHandler;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadPoolExecutor;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.TimeUnit;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.Logger;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport org.slf4j.LoggerFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * Mavs线程池,提供了钩子方法的默认实现
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsThreadPoolExecutor extends ThreadPoolExecutor {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? privatestaticfinal Logger LOGGER = LoggerFactory
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? .getLogger(MavsThreadPoolExecutor.class);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? long keepAliveTime, TimeUnit unit,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? handler);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? long keepAliveTime, TimeUnit unit,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? threadFactory);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? public MavsThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? long keepAliveTime, TimeUnit unit,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? RejectedExecutionHandler handler) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? threadFactory, handler);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? protectedvoid beforeExecute(Thread t, Runnable r) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.info("Thread["+ t.getName() +"]#beforeExecute:{}",
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? MavsThreadPoolStateMonitor.monitor(this));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super.beforeExecute(t, r);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? protectedvoid afterExecute(Runnable r, Throwable t) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super.afterExecute(r, t);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.info("Thread["+ Thread.currentThread().getName()
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? +"]EfterExecute:{}", MavsThreadPoolStateMonitor.monitor(this));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? if (t !=null) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? LOGGER.warn("Worker.runs.task.err", t);
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? @Override
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? protectedvoid terminated() {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? super.terminated();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? LOGGER.info("terminated:{}", MavsThreadPoolStateMonitor.monitor(this));
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



Java多线程札记6-ThreadPoolExecutor/ExecutorServicepackage com.landon.mavs.example.util;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadFactory;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.ThreadPoolExecutor;
Java多线程札记6-ThreadPoolExecutor/ExecutorServiceimport java.util.concurrent.TimeUnit;
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService/**
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * Mavs线程池状态Monitor
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService * @author landon
Java多线程札记6-ThreadPoolExecutor/ExecutorService *
Java多线程札记6-ThreadPoolExecutor/ExecutorService*/
Java多线程札记6-ThreadPoolExecutor/ExecutorServicepublicclass MavsThreadPoolStateMonitor {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? /**
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? *
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? * 线程池状态监视
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? *
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? * @param executor
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? * @return
Java多线程札记6-ThreadPoolExecutor/ExecutorService???? */
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? publicstatic String monitor(ThreadPoolExecutor executor) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? if (executor ==null) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? thrownew NullPointerException();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 核心线程数
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? int corePoolSize = executor.getCorePoolSize();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 最大线程数
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? int maximumPoolSize = executor.getMaximumPoolSize();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 线程保持活动时间
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? long keepAliveTime = executor.getKeepAliveTime(TimeUnit.MILLISECONDS);
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 当前线程数
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? int poolSize = executor.getPoolSize();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 返回活跃(正在执行任务)的近似线程数
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? int activeThreadCount = executor.getActiveCount();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 返回曾经同时位于池中的最大线程数(包括已被回收的worker线程计数)
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? int largestPoolSize = executor.getLargestPoolSize();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 已完成执行的近似任务总数
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? long completedTaskCount = executor.getCompletedTaskCount();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 曾计划完成的近似任务总数(completedTaskCount + 工作队列大小 + 正在执行任务的worker线程数目)
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? long taskCount = executor.getTaskCount();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 工作队列大小
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? int workQueueSize = executor.getQueue().size();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 是否在非RUNNING状态下
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? boolean isShutdown = executor.isShutdown();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 是否是TERMINATED状态
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? boolean isTerminated = executor.isTerminated();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? // 是否是SHUTDOWN或者STOP状态
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? boolean isTerminating = executor.isTerminating();
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? String executorName ="Default-ThreadPoolExecutor";
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? ThreadFactory factory = executor.getThreadFactory();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? if (factory !=null&& factory instanceof MavsThreadFactory) {
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????? executorName = ((MavsThreadFactory) factory).getNamePrefix();
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????? return executorName +" [corePoolSize="+ corePoolSize
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? +", maximumPoolSize="+ maximumPoolSize +", keepAliveTime="
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + keepAliveTime +", poolSize="+ poolSize
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? +", activeThreadCount="+ activeThreadCount
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? +", largestPoolSize="+ largestPoolSize
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? +", completedTaskCount="+ completedTaskCount +", taskCount="
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + taskCount +", workQueueSize="+ workQueueSize
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? +", isShutdown="+ isShutdown +", isTerminated="
Java多线程札记6-ThreadPoolExecutor/ExecutorService??????????????? + isTerminated +", isTerminating="+ isTerminating +"]";
Java多线程札记6-ThreadPoolExecutor/ExecutorService??? }
Java多线程札记6-ThreadPoolExecutor/ExecutorService}
Java多线程札记6-ThreadPoolExecutor/ExecutorService



三.总结:
????本篇结合jdk源码重点讲述了ThreadPoolExecutor和ExecutorService.另外提供了本人封装的一些线程池相关的库源码.

热点排行