java 线程池 嵌套_【并发】Java线程池的相关知识

是什么

线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。

解决问题

降低资源的消耗(重复利用)

提高响应速度 (并行执行)

提高线程的可关联性

组成部分

corePoolSize 线程池的基本大小

maximumPoolSize 线程池最大数量

keepAliveTime 线程活动保持的时间

taskQueue

ArrayBlockingQueue 基于数据数据结构的有界阻塞队列

LinkedBlockingQueue 链表的阻塞队列

SynchronousQueue 不存储元素的阻塞队列

PriorityBlockingQueue 优先级的无限队列

ThreadFactory 创建线程工厂

RejectExecutionHandler

AbortPolicy 直接抛异常

CallerRunsPolicy 只用调用者所在线程来运行任务

DiscardPolicy 丢弃队列里最近一个任务,并执行当前任务

DiscardOldestPolicy 不处理,丢失掉

如何使用

创建线程池

/**

* Creates a new {@code ThreadPoolExecutor} with the given initial

* parameters.

*

* @param corePoolSize the number of threads to keep in the pool, even

* if they are idle, unless {@code allowCoreThreadTimeOut} is set

* @param maximumPoolSize the maximum number of threads to allow in the

* pool

* @param keepAliveTime when the number of threads is greater than

* the core, this is the maximum time that excess idle threads

* will wait for new tasks before terminating.

* @param unit the time unit for the {@code keepAliveTime} argument

* @param workQueue the queue to use for holding tasks before they are

* executed. This queue will hold only the {@code Runnable}

* tasks submitted by the {@code execute} method.

* @param threadFactory the factory to use when the executor

* creates a new thread

* @param handler the handler to use when execution is blocked

* because the thread bounds and queue capacities are reached

* @throws IllegalArgumentException if one of the following holds:

* {@code corePoolSize < 0}

* {@code keepAliveTime < 0}

* {@code maximumPoolSize <= 0}

* {@code maximumPoolSize < corePoolSize}

* @throws NullPointerException if {@code workQueue}

* or {@code threadFactory} or {@code handler} is null

*/

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue workQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler) {

if (corePoolSize < 0 ||

maximumPoolSize <= 0 ||

maximumPoolSize < corePoolSize ||

keepAliveTime < 0)

throw new IllegalArgumentException();

if (workQueue == null || threadFactory == null || handler == null)

throw new NullPointerException();

this.corePoolSize = corePoolSize;

this.maximumPoolSize = maximumPoolSize;

this.workQueue = workQueue;

this.keepAliveTime = unit.toNanos(keepAliveTime);

this.threadFactory = threadFactory;

this.handler = handler;

}

执行

public void execute(Runnable command) {

.....

}

工作流程

执行流程

5137d755f509cd93369ceb2b56c9c51c.png

转载《java并行编程的艺术》一张图(这张图非常详细)

aaddfab6ca7507d9afc48a395b64e2e1.png

提交一个新任务

判断核心线程池是否执行任务,如果不是,则创建新线程来执行任务

如果线程池都在执行,则判断队列是否满,如果没有满,则将任务放到队列中

如果队列满了,并且没有线程池线程个数小于最大个数,则创建新的线程执行任务

如果队列满了,并且也达到了线程池的最大个数,则走线程池的拒绝策略

其他

线程池的监控

目标:方便查看线程池的状态

参数

taskCount:线程池需要执行的任务数量

completedTaskCount:线程池在运行过程中已完成的任务数量

largestPoolSize线程池里曾经创建过最大的线程数量

getPoolSize 线程池的线程数量

getActiveCount 获取活动的线程数

自定义实现线程池监控

通过继承线程池来自定义线程池,然后重写beforeExecute、afterExecute、terminated方法

思维导图

ebf182f5a66e7bd04694b861cf9a8c3d.png

参考资料

《java并行编程的艺术》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值