Java线程池实现原理
来源:互联网 发布:大学生遭遇网络诈骗 编辑:程序博客网 时间:2024/05/20 13:04
参数配置
核心池大小、最大池大小
/** * Core pool size is the minimum number of workers to keep alive * (and not allow to time out etc) unless allowCoreThreadTimeOut * is set, in which case the minimum is zero. */ private volatile int corePoolSize; /** * Maximum pool size. Note that the actual maximum is internally * bounded by CAPACITY. */ private volatile int maximumPoolSize; /** * Timeout in nanoseconds for idle threads waiting for work. * Threads use this timeout when there are more than corePoolSize * present or if allowCoreThreadTimeOut. Otherwise they wait * forever for new work. */ private volatile long keepAliveTime; /** * If false (default), core threads stay alive even when idle. * If true, core threads use keepAliveTime to time out waiting * for work. */ private volatile boolean allowCoreThreadTimeOut;
ThreadPoolExecutor根据这两个参数自动分配池大小。当线程数>corePoolSize 时,多于corePoolSize 的线程在超过keepAliveTime时间后,会终止执行。
另外如果设置了allowCoreThreadTimeOut为true,核心线程在空闲时间超过keepAliveTime 后终止执行。
线程分配流程:
- If fewer than corePoolSize threads are running, the Executor
- always prefers adding a new thread
- rather than queuing.
- If corePoolSize or more threads are running, the Executor
- always prefers queuing a request rather than adding a new
- thread.
- If a request cannot be queued, a new thread is created *unless this would exceed maximumPoolSize, in which case, the *task will be
- rejected.
如果有新请求提交到线程池,且运行中的线程数<corePoolSize,新建一个线程处理请求(即使有工作线程处于空闲状态)。
如果corePoolSize<=运行线程数,请求被放入队列。
如果corePoolSize<运行线程<maximumPoolSize,除非队列已满,才会新建一个线程处理请求。
如果运行线程达到maximumPoolSize且队列已满,将会执行rejectedExecution方法。
执行过程示意图
队列实现策略
SynchronousQueue:如果线程池中没有可用线程,将会创建一个新的线程。maximumPoolSize无限大,以防止提交的任务达到maximumPoolSize大小后被拒绝执行。
LinkedBlockingQueue:基于链表结构的阻塞队列,如果corePoolSize全部线程用完,在队列中等待,不会创建新线程,适合彼此独立的任务。
- ArrayBlockingQueue:配合有限的maximumPoolSizes,防止资源用尽,但难以协调控制。队列的大小和maximumPoolSizes需要权衡。
线程池的饱和策略
1.AbortPolicy
默认实现,直接抛出异常
/** * A handler for rejected tasks that throws a * {@code RejectedExecutionException}. */ public static class AbortPolicy implements RejectedExecutionHandler { /** * Creates an {@code AbortPolicy}. */ public AbortPolicy() { } /** * Always throws RejectedExecutionException. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task * @throws RejectedExecutionException always */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); } }
2.CallerRunsPolicy
executor调用线程直接执行被拒绝的任务(Runnable),除非调用线程executor被关闭,此时任务被丢弃。
/** * A handler for rejected tasks that runs the rejected task * directly in the calling thread of the {@code execute} method, * unless the executor has been shut down, in which case the task * is discarded. */ public static class CallerRunsPolicy implements RejectedExecutionHandler { /** * Creates a {@code CallerRunsPolicy}. */ public CallerRunsPolicy() { } /** * Executes task r in the caller's thread, unless the executor * has been shut down, in which case the task is discarded. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
3.DiscardPolicy
直接丢弃被拒绝的任务,不做任何处理.
/** * A handler for rejected tasks that silently discards the * rejected task. */ public static class DiscardPolicy implements RejectedExecutionHandler { /** * Creates a {@code DiscardPolicy}. */ public DiscardPolicy() { } /** * Does nothing, which has the effect of discarding task r. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { } }
4.DiscardOldestPolicy
从队列中丢弃被拒绝的请求,executor调用线程执行被拒绝的任务(Runnable),除非调用线程executor被关闭,此时任务被丢弃。
/** * A handler for rejected tasks that discards the oldest unhandled * request and then retries {@code execute}, unless the executor * is shut down, in which case the task is discarded. */ public static class DiscardOldestPolicy implements RejectedExecutionHandler { /** * Creates a {@code DiscardOldestPolicy} for the given executor. */ public DiscardOldestPolicy() { } /** * Obtains and ignores the next task that the executor * would otherwise execute, if one is immediately available, * and then retries execution of task r, unless the executor * is shut down, in which case task r is instead discarded. * * @param r the runnable task requested to be executed * @param e the executor attempting to execute this task */ public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
- Java线程池实现原理
- java线程池实现原理
- Java线程池实现原理
- Java线程实现原理
- Java 线程池的原理与实现
- java线程池的原理与实现
- java线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java版线程池实现与原理
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- Java 线程池的原理与实现
- postfix别名转发的设置
- #c##用户代码未处理ArgumentNullException
- TCP/IP协议,HTTP协议与webSocket协议区别
- PHP7 的新特性与性能优化
- 【Demo实例】Android FTP上传带进度条(优化后代码)
- Java线程池实现原理
- 毕业设计的坑--我的一个独立游戏的完整设计方案(2)
- 正则表达式基础
- 购物车动画(Android)
- JDK下载地址、SecureCRT中JDK安装和环境配置、SecureCRT窗口编程、linux下命令运行小程序
- 数据结构之二叉树基础三
- Android 6.0 通讯录及RecyclerView 复用错乱Bug
- Address already in use: JVM_Bind(解决方法)
- MySQL之centos6.5系统安装和配置mysql数据库