Java Java 关于Java线程池的解析与用法 inkOrCloud 2024-08-05 2025-10-07 继承关系图
进程池的几种常用创建类型 通过ThreadPoolExecutor构造函数创建 1 2 3 4 5 public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
主要参数
corePoolSize:最大核心线程数(必须)
maximumPoolSize:线程池最大总线程数(必须)
keepAliveTime:空闲线程的最长存活时间(必须)
unit:时间单位(必须)
workQueue:任务储存队列(必须)
ThreadFactory:线程工厂(默认Executors.DefaultThreadFactory)
handler:拒绝策略(默认AbortPolicy)
拒绝策略 拒绝策略是线程池无法接受新任务时的处理机制
1 2 3 public interface RejectedExecutionHandler { void rejectedExecution (Runnable var1, ThreadPoolExecutor var2) ; }
常见拒绝策略:
ThreadPoolExecutor.AbortPolicy:抛出一个RejectedExecutionException错误
1 2 3 4 5 6 7 8 public static class AbortPolicy implements RejectedExecutionHandler { public AbortPolicy () { } public void rejectedExecution (Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException ("Task " + r.toString() + " rejected from " + e.toString()); } }
ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列中最早加入的任务
1 2 3 4 5 6 7 8 9 10 11 12 public static class DiscardOldestPolicy implements RejectedExecutionHandler { public DiscardOldestPolicy () { } public void rejectedExecution (Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
ThreadExecutor.DiscardPolicy:丢弃当前任务,即不做任何操作
1 2 3 4 5 6 7 public static class DiscardPolicy implements RejectedExecutionHandler { public DiscardPolicy () { } public void rejectedExecution (Runnable r, ThreadPoolExecutor e) { } }
ThreadExecutor.CallerRunsPolicy:由提交任务的线程执行任务
1 2 3 4 5 6 7 8 9 10 11 public static class CallerRunsPolicy implements RejectedExecutionHandler { public CallerRunsPolicy () { } public void rejectedExecution (Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
通过Executors框架工厂函数创建
Executors.newFixedThreadPool:创建一个固定线程的线程
1 2 3 public static ExecutorService newFixedThreadPool (int nThreads) { return new ThreadPoolExecutor (nThreads, nThreads, 0L , TimeUnit.MILLISECONDS, new LinkedBlockingQueue ()); }
Executors.SingleThreadExecutor:创建一个单线程线程池
1 2 3 4 5 6 7 public static ExecutorService newSingleThreadExecutor () { return newSingleThreadExecutor(defaultThreadFactory()); } public static ExecutorService newSingleThreadExecutor (ThreadFactory threadFactory) { return new AutoShutdownDelegatedExecutorService (new ThreadPoolExecutor (1 , 1 , 0L , TimeUnit.MILLISECONDS, new LinkedBlockingQueue (), threadFactory)); }
Executors.newCachedThreadPool:创建一个无核心线程且线程上限为Integer.MAX_VALUE的线程池
1 2 3 public static ExecutorService newCachedThreadPool () { return new ThreadPoolExecutor (0 , Integer.MAX_VALUE, 60L , TimeUnit.SECONDS, new SynchronousQueue ()); }
注:以上工厂函数都可在最后加上ThreadFactory对象参数自定义线程工厂且任务队列最大容量为Integer.MAX_VALUE