发布时间:2025-12-10 23:09:57 浏览次数:1
Executor: java线程池框架的最上层父接口,在Executor中只有executor()方法,该方法表示提交Runnable类型线程池并执行。
ExecutorService: Executor的子接口,该接口中submit()方法可接收Runnable参数或Callable参数,在使用结束后使用shutdown()方法关闭线程池,不再接收新的任务。
AbstractExecutorService: ExecutorService的默认实现类。
ScheduledExecutorService: ExecutorService的子接口,可供定时任务调度的接口。
ScheduledThreadPoolExecutor: 提供了另一种线程池,延迟执行和周期性执行的线程池。
ThreadPoolExecutor: Java线程池最核心的一个类,该类继承自AbstractExecutorService主要功能是创建线程池,给任务分配线程资源,执行任务。
ThreadPoolExecutor有多个重载的构造方法,我们基于最完整的构造方法来分析每个参数的作用。
publicThreadPoolExecutor(intcorePoolSize,//核心线程池数量intmaximumPoolSize,//最大线程池数量longkeepAliveTime,//线程数大于核心线程池数,空闲线程的最大存活时间TimeUnitunit,//参数的时间单位BlockingQueue<Runnable>workQueue,//线程等待队列ThreadFactorythreadFactory,//用于设置创建线程的工厂RejectedExecutionHandlerhandler){//设置拒绝策略if(corePoolSize<0||maximumPoolSize<=0||maximumPoolSize<corePoolSize||keepAliveTime<0)thrownewIllegalArgumentException();if(workQueue==null||threadFactory==null||handler==null)thrownewNullPointerException();this.acc=System.getSecurityManager()==null?null:AccessController.getContext();this.corePoolSize=corePoolSize;this.maximumPoolSize=maximumPoolSize;this.workQueue=workQueue;this.keepAliveTime=unit.toNanos(keepAliveTime);this.threadFactory=threadFactory;this.handler=handler;}publicstaticExecutorServicenewFixedThreadPool(intnThreads,ThreadFactorythreadFactory){returnnewThreadPoolExecutor(nThreads,nThreads,0L,TimeUnit.MILLISECONDS,newLinkedBlockingQueue<Runnable>(),threadFactory);}corePoolSize与maximumPoolSize相等,即其线程全为核心线程,是一个固定大小的线程池,是其优势;
keepAliveTime = 0 该参数默认对核心线程无效,而FixedThreadPool全部为核心线程;
workQueue 为LinkedBlockingQueue(无界阻塞队列),队列最大值为Integer.MAX_VALUE。如果任务提交速度持续大余任务处理速度,会造成队列大量阻塞。因为队列很大,很有可能在拒绝策略前,内存溢出。是其劣势;
FixedThreadPool的任务执行是无序的;
publicstaticExecutorServicenewSingleThreadExecutor(ThreadFactorythreadFactory){returnnewFinalizableDelegatedExecutorService(newThreadPoolExecutor(1,1,0L,TimeUnit.MILLISECONDS,newLinkedBlockingQueue<Runnable>(),threadFactory));}控制线程池中corePoolSize与maximumPoolSize都为1
FinalizableDelegatedExecutorService继承DelegatedExecutorService,DelegatedExecutorService最终继承AbstractExecutorService,该类是线程池的一个代理模式的实现,相比于ThreadPoolExecutor**一部分功能,形成线程池单例化。
publicstaticExecutorServicenewCachedThreadPool(ThreadFactorythreadFactory){returnnewThreadPoolExecutor(0,Integer.MAX_VALUE,60L,TimeUnit.SECONDS,newSynchronousQueue<Runnable>(),threadFactory);}corePoolSize = 0,maximumPoolSize = Integer.MAX_VALUE,即线程数量几乎无限制
keepAliveTime = 60s,60s后空闲线程自动结束
SynchronousQueue 为同步队列,入队出队必须同时传递,因为CachedThreadPool线程创建无限制,不会有队列等待
publicstaticScheduledExecutorServicenewScheduledThreadPool(intcorePoolSize,ThreadFactorythreadFactory){returnnewScheduledThreadPoolExecutor(corePoolSize,threadFactory);}newScheduledThreadPool为定长线程池,限定核心线程数
ScheduledThreadPoolExecutor方法中对线程池参数做了进一步的封装,设置maximumPoolSize = Integer.MAX_VALUE,keepAliveTime = 0
调用scheduleAtFixedRate()方法可进行周期性任务设置
publicstaticExecutorServicenewWorkStealingPool(intparallelism){returnnewForkJoinPool(parallelism,ForkJoinPool.defaultForkJoinWorkerThreadFactory,null,true);}ForkJoinPool继承AbstractExecutorService,ForkJoinPool可以充分利用多核cpu的优势,将一个任务拆分成多个“小任务”并行计算,提高任务的执行时间
关于Executor的原理是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。