在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的并发工具线程池
我们会分为以下几部分进行介绍:
我们在这一小节简单介绍一下线程池
首先我们先来介绍线程池的产生背景:
我们给出一张线程池基本图:

我们在这一小节根据线程池基本图来自定义一个线程池
我们先来介绍一下拒绝策略接口:
我们给出拒绝策略接口代码:
- // 拒绝策略
- // 这里采用T来代表接收任务类型,可能是Runnable类型也可能是其他类型线程
- // 这里的reject就是抽象方法,我们后续直接采用Lambda表达式重新构造即可
- // BlockingQueue是阻塞队列,我们在后续创建;task是任务,我们直接传入即可
- @FunctionalInterface
- interface RejectPolicy<T>{
- void reject(BlockingQueue<T> queue,T task);
- }
我们来介绍一下任务队列:
我们给出任务队列代码:
- class BlockingQueue<T>{
-
- //阻塞队列,存放任务
- private Deque<T> queue = new ArrayDeque<>();
- //队列的最大容量
- private int capacity;
- //锁
- private ReentrantLock lock = new ReentrantLock();
- //生产者条件变量
- private Condition fullWaitSet = lock.newCondition();
- //消费者条件变量
- private Condition emptyWaitSet = lock.newCondition();
-
- //构造方法
- public BlockingQueue(int capacity) {
- this.capacity = capacity;
- }
-
- //超时阻塞获取
- public T poll(long timeout, TimeUnit unit){
- lock.lock();
- //将时间转换为纳秒
- long nanoTime = unit.toNanos(timeout);
- try{
- while(queue.size() == 0){
- try {
- //等待超时依旧没有获取,返回null
- if(nanoTime <= 0){
- return null;
- }
- //awaitNanos方法返回的是剩余时间
- nanoTime = emptyWaitSet.awaitNanos(nanoTime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- T t = queue.pollFirst();
- fullWaitSet.signal();
- return t;
- }finally {
- lock.unlock();
- }
- }
-
- //阻塞获取
- public T take(){
- lock.lock();
- try{
- while(queue.size() == 0){
- try {
- emptyWaitSet.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- T t = queue.pollFirst();
- fullWaitSet.signal();
- return t;
- }finally {
- lock.unlock();
- }
- }
-
- //阻塞添加
- public void put(T t){
- lock.lock();
- try{
- while (queue.size() == capacity){
- try {
- System.out.println(Thread.currentThread().toString() + "等待加入任务队列:" + t.toString());
- fullWaitSet.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(Thread.currentThread().toString() + "加入任务队列:" + t.toString());
- queue.addLast(t);
- emptyWaitSet.signal();
- }finally {
- lock.unlock();
- }
- }
-
- //超时阻塞添加
- public boolean offer(T t,long timeout,TimeUnit timeUnit){
- lock.lock();
- try{
- long nanoTime = timeUnit.toNanos(timeout);
- while (queue.size() == capacity){
- try {
- if(nanoTime <= 0){
- System.out.println("等待超时,加入失败:" + t);
- return false;
- }
- System.out.println(Thread.currentThread().toString() + "等待加入任务队列:" + t.toString());
- nanoTime = fullWaitSet.awaitNanos(nanoTime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(Thread.currentThread().toString() + "加入任务队列:" + t.toString());
- queue.addLast(t);
- emptyWaitSet.signal();
- return true;
- }finally {
- lock.unlock();
- }
- }
-
- // 获得当前任务队列长度
- public int size(){
- lock.lock();
- try{
- return queue.size();
- }finally{
- lock.unlock();
- }
- }
-
- // 从形参接收拒绝策略的put方法
- public void tryPut(RejectPolicy<T> rejectPolicy,T task){
- lock.lock();
- try{
- if(queue.size() == capacity){
- rejectPolicy.reject(this,task);
- }else{
- System.out.println("加入任务队列:" + task);
- queue.addLast(task);
- emptyWaitSet.signal();
- }
- }finally {
- lock.unlock();
- }
- }
- }
我们来介绍一下线程池:
我们给出线程池代码:
- class ThreadPool{
-
- //阻塞队列
- BlockingQueue<Runnable> taskQue;
- //线程集合
- HashSet<Worker> workers = new HashSet<>();
- //拒绝策略
- private RejectPolicy<Runnable> rejectPolicy;
-
- //构造方法
- public ThreadPool(int coreSize,long timeout,TimeUnit timeUnit,int queueCapacity,RejectPolicy<Runnable> rejectPolicy){
- this.coreSize = coreSize;
- this.timeout = timeout;
- this.timeUnit = timeUnit;
- this.rejectPolicy = rejectPolicy;
- taskQue = new BlockingQueue<Runnable>(queueCapacity);
- }
-
- //线程数
- private int coreSize;
- //任务超时时间
- private long timeout;
- //时间单元
- private TimeUnit timeUnit;
-
- //线程池的执行方法
- public void execute(Runnable task){
- //当线程数大于等于coreSize的时候,将任务放入阻塞队列
- //当线程数小于coreSize的时候,新建一个Worker放入workers
- //注意workers类不是线程安全的, 需要加锁
- synchronized (workers){
- if(workers.size() >= coreSize){
- //死等
- //带超时等待
- //让调用者放弃执行任务
- //让调用者抛出异常
- //让调用者自己执行任务
- taskQue.tryPut(rejectPolicy,task);
- }else {
- Worker worker = new Worker(task);
- System.out.println(Thread.currentThread().toString() + "新增worker:" + worker + ",task:" + task);
- workers.add(worker);
- worker.start();
- }
- }
- }
-
- //工作类
- class Worker extends Thread{
-
- private Runnable task;
-
- public Worker(Runnable task){
- this.task = task;
- }
-
- @Override
- public void run() {
- //巧妙的判断
- while(task != null || (task = taskQue.poll(timeout,timeUnit)) != null){
- try{
- System.out.println(Thread.currentThread().toString() + "正在执行:" + task);
- task.run();
- }catch (Exception e){
-
- }finally {
- task = null;
- }
- }
- synchronized (workers){
- System.out.println(Thread.currentThread().toString() + "worker被移除:" + this.toString());
- workers.remove(this);
- }
- }
- }
- }
我们给出自定义线程池的测试代码:
- public class ThreadPoolTest {
- public static void main(String[] args) {
- // 注意:这里最后传入的参数,也就是下面一大溜的方法就是拒绝策略接口,我们可以任意选择,此外put和offer是已经封装的方法
- ThreadPool threadPool = new ThreadPool(1, 1000, TimeUnit.MILLISECONDS, 1, (queue,task)->{
- //死等
- // queue.put(task);
- //带超时等待
- // queue.offer(task, 1500, TimeUnit.MILLISECONDS);
- //让调用者放弃任务执行
- // System.out.println("放弃:" + task);
- //让调用者抛出异常
- // throw new RuntimeException("任务执行失败" + task);
- //让调用者自己执行任务
- task.run();
- });
- for (int i = 0; i <3; i++) {
- int j = i;
- threadPool.execute(()->{
- try {
- System.out.println(Thread.currentThread().toString() + "执行任务:" + j);
- Thread.sleep(1000L);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- });
- }
- }
- }
我们在这一小节将介绍一种新的模式WorkThread
首先我们给出Worker Thread的基本定义:
我们给出一种具体的解释:
首先我们先来展示没有使用Worker Thread所出现的问题:
- /*
-
- 例如我们采用newFixedThreadPool创建一个具有规定2的线程的线程池
- 如果我们不为他们分配职责,就有可能导致两个线程都处于等待状态而造成饥饿现象
-
- - 两个工人是同一个线程池中的两个线程
-
- - 他们要做的事情是:为客人点餐和到后厨做菜,这是两个阶段的工作
- - 客人点餐:必须先点完餐,等菜做好,上菜,在此期间处理点餐的工人必须等待
- - 后厨做菜:做菜
- - 比如工人A 处理了点餐任务,接下来它要等着 工人B 把菜做好,然后上菜
- - 但现在同时来了两个客人,这个时候工人A 和工人B 都去处理点餐了,这时没人做饭了,造成饥饿
-
- */
-
- /*实际代码展示*/
-
- public class TestDeadLock {
-
- static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡翅");
- static Random RANDOM = new Random();
-
- static String cooking() {
- return MENU.get(RANDOM.nextInt(MENU.size()));
- }
-
- public static void main(String[] args) {
-
- // 我们这里创建一个固定线程池,里面涵盖两个线程
- ExecutorService executorService = Executors.newFixedThreadPool(2);
-
- executorService.execute(() -> {
- log.debug("处理点餐...");
- Future<String> f = executorService.submit(() -> {
- log.debug("做菜");
- return cooking();
- });
- try {
- log.debug("上菜: {}", f.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- });
-
- // 开启下面代码即两人同时负责点餐
- /*
- executorService.execute(() -> {
- log.debug("处理点餐...");
- Future<String> f = executorService.submit(() -> {
- log.debug("做菜");
- return cooking();
- });
- try {
- log.debug("上菜: {}", f.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- });
- */
- }
- }
-
- /*正确运行*/
-
- 17:21:27.883 c.TestDeadLock [pool-1-thread-1] - 处理点餐...
- 17:21:27.891 c.TestDeadLock [pool-1-thread-2] - 做菜
- 17:21:27.891 c.TestDeadLock [pool-1-thread-1] - 上菜: 烤鸡翅
-
- /*代码注释后运行*/
-
- 17:08:41.339 c.TestDeadLock [pool-1-thread-2] - 处理点餐...
- 17:08:41.339 c.TestDeadLock [pool-1-thread-1] - 处理点餐...
如果想要解除之前的饥饿现象,正确的方法就是采用Worker Thread模式为他们分配角色,让他们只专属于一份工作:
- /*代码展示*/
-
- public class TestDeadLock {
-
- static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡翅");
- static Random RANDOM = new Random();
-
- static String cooking() {
- return MENU.get(RANDOM.nextInt(MENU.size()));
- }
-
- public static void main(String[] args) {
-
- // 我们这里创建两个线程池,分别包含一个线程,用于不同的分工
- ExecutorService waiterPool = Executors.newFixedThreadPool(1);
- ExecutorService cookPool = Executors.newFixedThreadPool(1);
-
- // 我们这里采用waiterPool线程池来处理点餐,采用cookPool来处理做菜
- waiterPool.execute(() -> {
- log.debug("处理点餐...");
- Future<String> f = cookPool.submit(() -> {
- log.debug("做菜");
- return cooking();
- });
- try {
- log.debug("上菜: {}", f.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- });
-
- // 无论多少线程他们都会正常运行
- waiterPool.execute(() -> {
- log.debug("处理点餐...");
- Future<String> f = cookPool.submit(() -> {
- log.debug("做菜");
- return cooking();
- });
- try {
- log.debug("上菜: {}", f.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- });
- }
- }
-
- /*结果展示*/
-
- 17:25:14.626 c.TestDeadLock [pool-1-thread-1] - 处理点餐...
- 17:25:14.630 c.TestDeadLock [pool-2-thread-1] - 做菜
- 17:25:14.631 c.TestDeadLock [pool-1-thread-1] - 上菜: 地三鲜
- 17:25:14.632 c.TestDeadLock [pool-1-thread-1] - 处理点餐...
- 17:25:14.632 c.TestDeadLock [pool-2-thread-1] - 做菜
- 17:25:14.632 c.TestDeadLock [pool-1-thread-1] - 上菜: 辣子鸡丁
最后我们来思考一下线程池大小的问题:
我们给出两种形式下的线程池大小规范:
- /*
- 通常采用 `cpu 核数 + 1` 能够实现最优的 CPU 利用率
- +1 是保证当线程由于页缺失故障(操作系统)或其它原因导致暂停时,额外的这个线程就能顶上去,保证 CPU 时钟周期不被浪费
- */
- /*
- CPU 不总是处于繁忙状态,例如,当你执行业务计算时,这时候会使用 CPU 资源
- 但当你执行 I/O 操作时、远程 RPC 调用时,包括进行数据库操作时,这时候 CPU 就闲下来了,你可以利用多线程提高它的利用率。
-
- 经验公式如下
-
- `线程数 = 核数 * 期望 CPU 利用率 * 总时间(CPU计算时间+等待时间) / CPU 计算时间`
-
- 例如 4 核 CPU 计算时间是 50% ,其它等待时间是 50%,期望 cpu 被 100% 利用,套用公式
-
- `4 * 100% * 100% / 50% = 8`
-
- 例如 4 核 CPU 计算时间是 10% ,其它等待时间是 90%,期望 cpu 被 100% 利用,套用公式
-
- `4 * 100% * 100% / 10% = 40`
- */
下面我们来介绍JDK中为我们提供的线程池设计
首先我们要知道JDK为我们提供的线程池都是通过Executors的方法来构造的
我们给出继承图:

其中我们所使用的线程创造类分为两种:
我们首先给出线程池状态的构造规则:
我们给出具体线程状态:
| 状态名 | 高3位 | 接收新任务 | 处理阻塞队列任务 | 说明 |
|---|---|---|---|---|
| RUNNING | 111 | Y | Y | 正常运行 |
| SHUTDOWN | 000 | N | Y | 不会接收新任务,但会处理阻塞队列剩余 任务 |
| STOP | 001 | N | N | 会中断正在执行的任务,并抛弃阻塞队列 任务 |
| TIDYING | 010 | 任务全执行完毕,活动线程为 0 即将进入 终结 | ||
| TERMINATED | 011 | 终结状态 |
从数字上比较,TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING (因为RUNNING为负数)
我们给出线程池中ThreadPoolExecutor最完善的构造方法的参数展示:
- public ThreadPoolExecutor(int corePoolSize,
- int maximumPoolSize,
- long keepAliveTime,
- TimeUnit unit,
- BlockingQueue
workQueue, - ThreadFactory threadFactory,
- RejectedExecutionHandler handler)
我们对上述各种类型进行一一介绍:
我们首先给出工作方式展示图:
线程池c-2,m=3
阻塞队列
核心线程1
核心线程2
救急线程1
任务1
任务2
size=2
任务3
任务4
我们对此进行简单解释:
线程池中刚开始没有线程,当一个任务提交给线程池后,线程池会创建一个新线程来执行任务。
当线程数达到 corePoolSize 并没有线程空闲,这时再加入任务,新加的任务会被加入workQueue 队列排 队,直到有空闲的线程。
如果队列选择了有界队列,那么任务超过了队列大小时,会创建 maximumPoolSize - corePoolSize 数目的线程来救急。
如果线程到达 maximumPoolSize 仍然有新任务这时会执行拒绝策略。
当高峰过去后,超过corePoolSize 的救急线程如果一段时间没有任务做,需要结束节省资源,这个时间由keepAliveTime和unit来控制。
拒绝策略 jdk 提供了 4 种实现,其它著名框架也提供了实现:
我们首先来介绍一下newFixedThreadPool:
我们给出构造方法:
- /*我们正常调用的方法*/
-
- // 我们只需要提供线程数量nThreads,就会创建一个大小为nThreads的线程池
- // 下面会返回一个相应配置的线程池,这里的核心线程和最大线程都是nThreads,就意味着没有救急线程,同时也不需要设置保存时间
- public static ExecutorService newFixedThreadPool(int nThreads) {
- return new ThreadPoolExecutor(nThreads, nThreads,
- 0L, TimeUnit.MILLISECONDS,
- new LinkedBlockingQueue<Runnable>());
- }
-
- /*底层实现方法*/
-
- // 这和我们前面的构造方法是完全相同的
- public ThreadPoolExecutor(int corePoolSize,
- int maximumPoolSize,
- long keepAliveTime,
- TimeUnit unit,
- BlockingQueue<Runnable> workQueue) {
- this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
- Executors.defaultThreadFactory(), defaultHandler);
- }
-
- /*默认工厂以及默认构造线程的方法*/
-
- // 对应上述构造方法中的默认工厂以及线程构造,主要是控制了命名以及优先级并设置不为守护线程等内容
- DefaultThreadFactory() {
- SecurityManager s = System.getSecurityManager();
- group = (s != null) ? s.getThreadGroup() :
- Thread.currentThread().getThreadGroup();
- namePrefix = "pool-" +
- poolNumber.getAndIncrement() +
- "-thread-";
- }
-
- public Thread newThread(Runnable r) {
- Thread t = new Thread(group, r,
- namePrefix + threadNumber.getAndIncrement(),
- 0);
- if (t.isDaemon())
- t.setDaemon(false);
- if (t.getPriority() != Thread.NORM_PRIORITY)
- t.setPriority(Thread.NORM_PRIORITY);
- return t;
- }
-
- /*默认拒绝策略:抛出异常*/
-
- private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
我们最后给出具体特点:
我们首先来介绍一下newCachedThreadPool:
我们给出构造方法:
- /*调用方法*/
-
- public static ExecutorService newCachedThreadPool() {
- return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
- 60L, TimeUnit.SECONDS,
- new SynchronousQueue<Runnable>());
- }
-
- /*测试代码*/
-
- SynchronousQueue<Integer> integers = new SynchronousQueue<>();
- new Thread(() -> {
- try {
- log.debug("putting {} ", 1);
- integers.put(1);
- log.debug("{} putted...", 1);
- log.debug("putting...{} ", 2);
- integers.put(2);
- log.debug("{} putted...", 2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- },"t1").start();
- sleep(1);
- new Thread(() -> {
- try {
- log.debug("taking {}", 1);
- integers.take();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- },"t2").start();
- sleep(1);
- new Thread(() -> {
- try {
- log.debug("taking {}", 2);
- integers.take();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- },"t3").start();
-
- /*输出结果*/
-
- 11:48:15.500 c.TestSynchronousQueue [t1] - putting 1
- 11:48:16.500 c.TestSynchronousQueue [t2] - taking 1
- 11:48:16.500 c.TestSynchronousQueue [t1] - 1 putted...
- 11:48:16.500 c.TestSynchronousQueue [t1] - putting...2
- 11:48:17.502 c.TestSynchronousQueue [t3] - taking 2
- 11:48:17.503 c.TestSynchronousQueue [t1] - 2 putted...
我们给出newCachedThreadPool的特点:
我们先来简单介绍一下newSingleThreadExecutor:
我们给出构造方法:
- /*构造方法*/
-
- public static ExecutorService newSingleThreadExecutor() {
- return new FinalizableDelegatedExecutorService
- (new ThreadPoolExecutor(1, 1,
- 0L, TimeUnit.MILLISECONDS,
- new LinkedBlockingQueue<Runnable>()));
- }
我们给出newSingleThreadExecutor的特点:
下面我们来介绍多种提交任务的执行方法:
- /*介绍*/
-
- // 执行任务
- void execute(Runnable command);
-
- // 提交任务 task,用返回值 Future 获得任务执行结果
- <T> Future<T> submit(Callable<T> task);
-
- // 提交 tasks 中所有任务
- <T> List<Future<T>> invokeAll(Collection extends Callable
> tasks) - throws InterruptedException;
-
- // 提交 tasks 中所有任务,带超时时间,时间超时后,会放弃执行后面的任务
- <T> List<Future<T>> invokeAll(Collection extends Callable
> tasks, - long timeout, TimeUnit unit)
- throws InterruptedException;
-
- // 提交 tasks 中所有任务,哪个任务先成功执行完毕,返回此任务执行结果,其它任务取消
- <T> T invokeAny(Collection<? extends Callable<T>> tasks)
- throws InterruptedException, ExecutionException;
-
- // 提交 tasks 中所有任务,哪个任务先成功执行完毕,返回此任务执行结果,其它任务取消,带超时时间
- <T> T invokeAny(Collection<? extends Callable<T>> tasks,
- long timeout, TimeUnit unit)
- throws InterruptedException, ExecutionException, TimeoutException;
-
- /*submit*/
-
- // 测试代码
- private static void method1(ExecutorService pool) throws InterruptedException, ExecutionException {
- Future<String> future = pool.submit(() -> {
- log.debug("running");
- Thread.sleep(1000);
- return "ok";
- });
-
- log.debug("{}", future.get());
- }
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- ExecutorService pool = Executors.newFixedThreadPool(1);
- method1(pool);
- }
-
- // 结果
- 18:36:58.033 c.TestSubmit [pool-1-thread-1] - running
- 18:36:59.034 c.TestSubmit [main] - ok
-
- /*invokeAll*/
-
- // 测试代码
- private static void method2(ExecutorService pool) throws InterruptedException {
- List<Future<String>> futures = pool.invokeAll(Arrays.asList(
- () -> {
- log.debug("begin");
- Thread.sleep(1000);
- return "1";
- },
- () -> {
- log.debug("begin");
- Thread.sleep(500);
- return "2";
- },
- () -> {
- log.debug("begin");
- Thread.sleep(2000);
- return "3";
- }
- ));
-
- futures.forEach( f -> {
- try {
- log.debug("{}", f.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- });
- }
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- ExecutorService pool = Executors.newFixedThreadPool(1);
- method2(pool);
- }
-
- // 结果
- 19:33:16.530 c.TestSubmit [pool-1-thread-1] - begin
- 19:33:17.530 c.TestSubmit [pool-1-thread-1] - begin
- 19:33:18.040 c.TestSubmit [pool-1-thread-1] - begin
- 19:33:20.051 c.TestSubmit [main] - 1
- 19:33:20.051 c.TestSubmit [main] - 2
- 19:33:20.051 c.TestSubmit [main] - 3
-
- /*invokeAny*/
- private static void method3(ExecutorService pool) throws InterruptedException, ExecutionException {
- String result = pool.invokeAny(Arrays.asList(
- () -> {
- log.debug("begin 1");
- Thread.sleep(1000);
- log.debug("end 1");
- return "1";
- },
- () -> {
- log.debug("begin 2");
- Thread.sleep(500);
- log.debug("end 2");
- return "2";
- },
- () -> {
- log.debug("begin 3");
- Thread.sleep(2000);
- log.debug("end 3");
- return "3";
- }
- ));
- log.debug("{}", result);
- }
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- ExecutorService pool = Executors.newFixedThreadPool(3);
- //ExecutorService pool = Executors.newFixedThreadPool(1);
- method3(pool);
- }
-
- // 结果
- 19:44:46.314 c.TestSubmit [pool-1-thread-1] - begin 1
- 19:44:46.314 c.TestSubmit [pool-1-thread-3] - begin 3
- 19:44:46.314 c.TestSubmit [pool-1-thread-2] - begin 2
- 19:44:46.817 c.TestSubmit [pool-1-thread-2] - end 2
- 19:44:46.817 c.TestSubmit [main] - 2
-
- 19:47:16.063 c.TestSubmit [pool-1-thread-1] - begin 1
- 19:47:17.063 c.TestSubmit [pool-1-thread-1] - end 1
- 19:47:17.063 c.TestSubmit [pool-1-thread-1] - begin 2
- 19:47:17.063 c.TestSubmit [main] - 1
我们给出关闭线程池的多种方法:
- /*SHUTDOWN*/
-
- /*
- 线程池状态变为 SHUTDOWN
- - 不会接收新任务
- - 但已提交任务会执行完
- - 此方法不会阻塞调用线程的执行
- */
- void shutdown();
-
- public void shutdown() {
- final ReentrantLock mainLock = this.mainLock;
- mainLock.lock();
- try {
- checkShutdownAccess();
- // 修改线程池状态
- advanceRunState(SHUTDOWN);
- // 仅会打断空闲线程
- interruptIdleWorkers();
- onShutdown(); // 扩展点 ScheduledThreadPoolExecutor
- } finally {
- mainLock.unlock();
- }
- // 尝试终结(没有运行的线程可以立刻终结,如果还有运行的线程也不会等)
- tryTerminate();
- }
-
- /*shutdownNow*/
-
- /*
- 线程池状态变为 STOP
- - 不会接收新任务
- - 会将队列中的任务返回
- - 并用 interrupt 的方式中断正在执行的任务
- */
- List<Runnable> shutdownNow();
-
- public List<Runnable> shutdownNow() {
- List<Runnable> tasks;
- final ReentrantLock mainLock = this.mainLock;
- mainLock.lock();
- try {
- checkShutdownAccess();
- // 修改线程池状态
- advanceRunState(STOP);
- // 打断所有线程
- interruptWorkers();
- // 获取队列中剩余任务
- tasks = drainQueue();
- } finally {
- mainLock.unlock();
- }
- // 尝试终结
- tryTerminate();
- return tasks;
- }
-
- /*其他方法*/
-
- // 不在 RUNNING 状态的线程池,此方法就返回 true
- boolean isShutdown();
- // 线程池状态是否是 TERMINATED
- boolean isTerminated();
- // 调用 shutdown 后,由于调用线程并不会等待所有任务运行结束,因此如果它想在线程池 TERMINATED 后做些事情,可以利用此方法等待
- // 一般task是Callable类型的时候不用此方法,因为futureTask.get方法自带等待功能。
- boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
-
- /*测试shutdown、shutdownNow、awaitTermination*/
-
- // 代码
- @Slf4j(topic = "c.TestShutDown")
- public class TestShutDown {
-
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- ExecutorService pool = Executors.newFixedThreadPool(2);
-
- Future<Integer> result1 = pool.submit(() -> {
- log.debug("task 1 running...");
- Thread.sleep(1000);
- log.debug("task 1 finish...");
- return 1;
- });
-
- Future<Integer> result2 = pool.submit(() -> {
- log.debug("task 2 running...");
- Thread.sleep(1000);
- log.debug("task 2 finish...");
- return 2;
- });
-
- Future<Integer> result3 = pool.submit(() -> {
- log.debug("task 3 running...");
- Thread.sleep(1000);
- log.debug("task 3 finish...");
- return 3;
- });
-
- log.debug("shutdown");
- pool.shutdown();
- // pool.awaitTermination(3, TimeUnit.SECONDS);
- // List<Runnable> runnables = pool.shutdownNow();
- // log.debug("other.... {}" , runnables);
- }
- }
-
- // 结果
- #shutdown依旧会执行剩下的任务
- 20:09:13.285 c.TestShutDown [main] - shutdown
- 20:09:13.285 c.TestShutDown [pool-1-thread-1] - task 1 running...
- 20:09:13.285 c.TestShutDown [pool-1-thread-2] - task 2 running...
- 20:09:14.293 c.TestShutDown [pool-1-thread-2] - task 2 finish...
- 20:09:14.293 c.TestShutDown [pool-1-thread-1] - task 1 finish...
- 20:09:14.293 c.TestShutDown [pool-1-thread-2] - task 3 running...
- 20:09:15.303 c.TestShutDown [pool-1-thread-2] - task 3 finish...
- #shutdownNow立刻停止所有任务
- 20:11:11.750 c.TestShutDown [main] - shutdown
- 20:11:11.750 c.TestShutDown [pool-1-thread-1] - task 1 running...
- 20:11:11.750 c.TestShutDown [pool-1-thread-2] - task 2 running...
- 20:11:11.750 c.TestShutDown [main] - other.... [java.util.concurrent.FutureTask@66d33a]
在『任务调度线程池』功能加入之前(JDK1.3),可以使用 java.util.Timer 来实现定时功能
Timer 的优点在于简单易用,但由于所有任务都是由同一个线程来调度,因此所有任务都是串行执行的
同一时间只能有一个任务在执行,前一个 任务的延迟或异常都将会影响到之后的任务。
我们首先首先给出Timer的使用:
- /*Timer使用代码*/
-
- public static void main(String[] args) {
- Timer timer = new Timer();
- TimerTask task1 = new TimerTask() {
- @Override
- public void run() {
- log.debug("task 1");
- sleep(2);
- }
- };
- TimerTask task2 = new TimerTask() {
- @Override
- public void run() {
- log.debug("task 2");
- }
- };
- // 使用 timer 添加两个任务,希望它们都在 1s 后执行
- // 但由于 timer 内只有一个线程来顺序执行队列中的任务,因此『任务1』的延时,影响了『任务2』的执行
- timer.schedule(task1, 1000);
- timer.schedule(task2, 1000);
- }
-
- /*结果*/
-
- 20:46:09.444 c.TestTimer [main] - start...
- 20:46:10.447 c.TestTimer [Timer-0] - task 1
- 20:46:12.448 c.TestTimer [Timer-0] - task 2
我们再给出 ScheduledExecutorService 的改写格式:
- /*ScheduledExecutorService代码书写*/
-
- ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
- // 添加两个任务,希望它们都在 1s 后执行
- executor.schedule(() -> {
- System.out.println("任务1,执行时间:" + new Date());
- try { Thread.sleep(2000); } catch (InterruptedException e) { }
- }, 1000, TimeUnit.MILLISECONDS);
- executor.schedule(() -> {
- System.out.println("任务2,执行时间:" + new Date());
- }, 1000, TimeUnit.MILLISECONDS);
-
- /*结果*/
-
- 任务1,执行时间:Thu Jan 03 12:45:17 CST 2019
- 任务2,执行时间:Thu Jan 03 12:45:17 CST 2019
我们对其再进行更细节的测试分析:
- /*scheduleAtFixedRate:任务执行时间超过了间隔时间*/
-
- // 代码
- ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
- log.debug("start...");
- pool.scheduleAtFixedRate(() -> {
- log.debug("running...");
- sleep(2);
- }, 1, 1, TimeUnit.SECONDS);
-
- // 结果
- 21:44:30.311 c.TestTimer [main] - start...
- 21:44:31.360 c.TestTimer [pool-1-thread-1] - running...
- 21:44:33.361 c.TestTimer [pool-1-thread-1] - running...
- 21:44:35.362 c.TestTimer [pool-1-thread-1] - running...
- 21:44:37.362 c.TestTimer [pool-1-thread-1] - running...
-
- /*scheduleWithFixedDelay:在任务完成的基础上,设置时间间隔*/
-
- // 代码
- ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
- log.debug("start...");
- pool.scheduleWithFixedDelay(()-> {
- log.debug("running...");
- sleep(2);
- }, 1, 1, TimeUnit.SECONDS);
-
- // 结果
- 21:40:55.078 c.TestTimer [main] - start...
- 21:40:56.140 c.TestTimer [pool-1-thread-1] - running...
- 21:40:59.143 c.TestTimer [pool-1-thread-1] - running...
- 21:41:02.145 c.TestTimer [pool-1-thread-1] - running...
- 21:41:05.147 c.TestTimer [pool-1-thread-1] - running...
我们给出ScheduledExecutorService适用范围:
我们针对异常在之前一般会选择抛出或者无视,但这里我们给出应对方法:
- /*try-catch*/
-
- // 代码
- ExecutorService pool = Executors.newFixedThreadPool(1);
- pool.submit(() -> {
- try {
- log.debug("task1");
- int i = 1 / 0;
- } catch (Exception e) {
- log.error("error:", e);
- }
- });
-
- // 结果
- 21:59:04.558 c.TestTimer [pool-1-thread-1] - task1
- 21:59:04.562 c.TestTimer [pool-1-thread-1] - error:
- java.lang.ArithmeticException: / by zero
- at cn.itcast.n8.TestTimer.lambda$main$0(TestTimer.java:28)
- at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
- at java.util.concurrent.FutureTask.run(FutureTask.java:266)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
- at java.lang.Thread.run(Thread.java:748)
-
- /*Future返回*/
-
- // 我们在之前的提交任务中已经学习了submit等提交方法,当发异常时,这类返回对象Future将会返回异常信息
-
- // 代码
- ExecutorService pool = Executors.newFixedThreadPool(1);
- Future<Boolean> f = pool.submit(() -> {
- log.debug("task1");
- int i = 1 / 0;
- return true;
- });
- log.debug("result:{}", f.get());
-
- // 结果
- 21:54:58.208 c.TestTimer [pool-1-thread-1] - task1
- Exception in thread "main" java.util.concurrent.ExecutionException:
- java.lang.ArithmeticException: / by zero
- at java.util.concurrent.FutureTask.report(FutureTask.java:122)
- at java.util.concurrent.FutureTask.get(FutureTask.java:192)
- at cn.itcast.n8.TestTimer.main(TestTimer.java:31)
- Caused by: java.lang.ArithmeticException: / by zero
- at cn.itcast.n8.TestTimer.lambda$main$0(TestTimer.java:28)
- at java.util.concurrent.FutureTask.run(FutureTask.java:266)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
- at java.lang.Thread.run(Thread.java:748)
我们进行一个简单的实例展示:
- /*任务:在每周四下午六点执行方法*/
-
- /* 代码 */
-
- // 获得当前时间
- LocalDateTime now = LocalDateTime.now();
- // 获取本周四 18:00:00.000
- LocalDateTime thursday =
- now.with(DayOfWeek.THURSDAY).withHour(18).withMinute(0).withSecond(0).withNano(0);
- // 如果当前时间已经超过 本周四 18:00:00.000, 那么找下周四 18:00:00.000
- if(now.compareTo(thursday) >= 0) {
- thursday = thursday.plusWeeks(1);
- }
- // 计算时间差,即延时执行时间
- long initialDelay = Duration.between(now, thursday).toMillis();
- // 计算间隔时间,即 1 周的毫秒值
- long oneWeek = 7 * 24 * 3600 * 1000;
- ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
- System.out.println("开始时间:" + new Date());
- executor.scheduleAtFixedRate(() -> {
- System.out.println("执行时间:" + new Date());
- }, initialDelay, oneWeek, TimeUnit.MILLISECONDS);
下面我们来介绍Tomcat中所使用的线程池
我们首先给出Tomcat线程运作的展示图:
Connector->NIO EndPoint
Executor
有读
有读
socketProcessor
socketProcessor
LimitLatch
Acceptor
SocketChannel 1
SocketChannel 2
Poller
worker1
worker2
我们针对上述图给出对应解释:
我们需要注意Tomcat针对原本JDK提供的线程池进行了部分修改:
Tomcat 线程池扩展了 ThreadPoolExecutor,行为稍有不同
我们给出Tomcat相关配置信息:
| 配置项 | 默认值 | 说明 |
|---|---|---|
acceptorThreadCount | 1 | acceptor 线程数量 |
pollerThreadCount | 1 | poller 线程数量 |
minSpareThreads | 10 | 核心线程数,即 corePoolSize |
maxThreads | 200 | 最大线程数,即 maximumPoolSize |
executor | - | Executor 名称,用来引用下面的 Executor |
| 配置项 | 默认值 | 说明 |
|---|---|---|
threadPriority | 5 | 线程优先级 |
deamon | true | 是否守护线程 |
minSpareThreads | 25 | 核心线程数,即corePoolSize |
maxThreads | 200 | 最大线程数,即 maximumPoolSize |
maxIdleTime | 60000 | 线程生存时间,单位是毫秒,默认值即 1 分钟 |
maxQueueSize | Integer.MAX_VALUE | 队列长度 |
prestartminSpareThreads | false | 核心线程是否在服务器启动时启动 |
这一小节我们来介绍Fork/Join线程池思想
我们首先来简单介绍一下Fork/Join:
我们来介绍一下任务拆分:
我们给出Fork/Join的一些思想:
Fork/Join 在分治的基础上加入了多线程,可以把每个任务的分解和合并交给不同的线程来完成,进一步提升了运算效率
Fork/Join 默认会创建与 cpu 核心数大小相同的线程池
我们给出一个简单的应用题材来展示Fork/Join:
我们给出对应代码:
- /*求和代码*/
-
- public static void main(String[] args) {
- ForkJoinPool pool = new ForkJoinPool(4);
- System.out.println(pool.invoke(new AddTask1(5)));
- }
-
- @Slf4j(topic = "c.AddTask")
- class AddTask1 extends RecursiveTask<Integer> {
- int n;
- public AddTask1(int n) {
- this.n = n;
- }
- @Override
- public String toString() {
- return "{" + n + '}';
- }
- @Override
- protected Integer compute() {
- // 如果 n 已经为 1,可以求得结果了
- if (n == 1) {
- log.debug("join() {}", n);
- return n;
- }
-
- // 将任务进行拆分(fork)
- AddTask1 t1 = new AddTask1(n - 1);
- t1.fork();
- log.debug("fork() {} + {}", n, t1);
-
- // 合并(join)结果
- int result = n + t1.join();
- log.debug("join() {} + {} = {}", n, t1, result);
- return result;
- }
- }
-
- /*求和结果*/
-
- [ForkJoinPool-1-worker-0] - fork() 2 + {1}
- [ForkJoinPool-1-worker-1] - fork() 5 + {4}
- [ForkJoinPool-1-worker-0] - join() 1
- [ForkJoinPool-1-worker-0] - join() 2 + {1} = 3
- [ForkJoinPool-1-worker-2] - fork() 4 + {3}
- [ForkJoinPool-1-worker-3] - fork() 3 + {2}
- [ForkJoinPool-1-worker-3] - join() 3 + {2} = 6
- [ForkJoinPool-1-worker-2] - join() 4 + {3} = 10
- [ForkJoinPool-1-worker-1] - join() 5 + {4} = 15
- 15
-
- /*改进代码*/
-
- public static void main(String[] args) {
- ForkJoinPool pool = new ForkJoinPool(4);
- System.out.println(pool.invoke(new AddTask3(1, 10)));
- }
-
- class AddTask3 extends RecursiveTask<Integer> {
-
- int begin;
- int end;
- public AddTask3(int begin, int end) {
- this.begin = begin;
- this.end = end;
- }
- @Override
- public String toString() {
- return "{" + begin + "," + end + '}';
- }
- @Override
- protected Integer compute() {
- // 5, 5
- if (begin == end) {
- log.debug("join() {}", begin);
- return begin;
- }
- // 4, 5
- if (end - begin == 1) {
- log.debug("join() {} + {} = {}", begin, end, end + begin);
- return end + begin;
- }
-
- // 1 5
- int mid = (end + begin) / 2; // 3
- AddTask3 t1 = new AddTask3(begin, mid); // 1,3
- t1.fork();
- AddTask3 t2 = new AddTask3(mid + 1, end); // 4,5
- t2.fork();
- log.debug("fork() {} + {} = ?", t1, t2);
- int result = t1.join() + t2.join();
- log.debug("join() {} + {} = {}", t1, t2, result);
- return result;
- }
- }
-
- /*改进结果*/
-
- [ForkJoinPool-1-worker-0] - join() 1 + 2 = 3
- [ForkJoinPool-1-worker-3] - join() 4 + 5 = 9
- [ForkJoinPool-1-worker-0] - join() 3
- [ForkJoinPool-1-worker-1] - fork() {1,3} + {4,5} = ?
- [ForkJoinPool-1-worker-2] - fork() {1,2} + {3,3} = ?
- [ForkJoinPool-1-worker-2] - join() {1,2} + {3,3} = 6
- [ForkJoinPool-1-worker-1] - join() {1,3} + {4,5} = 15
- 15