Interview Assault 35: How to judge that the thread pool has executed all tasks?

In many scenarios, we need to wait for all tasks in the thread pool to be executed before proceeding to the next step. For the thread Thread, it is very easy to implement, adding a join method will solve it, but the judgment of the thread pool is more troublesome.

This article provides 4 methods to determine whether the thread pool task has been executed:

  1. Use the isTerminated method to judge.
  2. Use the getCompletedTaskCount method to judge.
  3. Use CountDownLatch to judge.
  4. Use the CyclicBarrier judgment.

Let's take a look at them one by one.

question of indecision

If you do not judge whether the thread pool has been executed, the following problems will occur, as shown in the following code:

import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolCompleted {
    public static void main(String[] args) {
        // 创建线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
                0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024));
        // 添加任务
        addTask(threadPool);
				// 打印结果
        System.out.println("线程池任务执行完成!");
    }
  
    /**
     * 给线程池添加任务
     */
    private static void addTask(ThreadPoolExecutor threadPool) {
        // 任务总数
        final int taskCount = 5;
        // 添加任务
        for (int i = 0; i < taskCount; i++) {
            final int finalI = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 随机休眠 0-4s
                        int sleepTime = new Random().nextInt(5);
                        TimeUnit.SECONDS.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(String.format("任务%d执行完成", finalI));
                }
            });
        }
    }
}
复制代码

The execution results of the above program are as follows: image.pngFrom the above execution results, it can be seen that the program first prints "Thread pool task execution completed!", and then continues to execute thread pool tasks one after another. The result of this disordered execution order is not ours. desired result. The result we want is to wait for all tasks to be executed before printing the "Thread pool task execution completed!" message.

The reason for the above problem is that the main thread main and the thread pool are executed concurrently, so when the thread pool has not finished executing, the print result code of the main thread has already been executed. To solve this problem, it is necessary to determine whether all the tasks of the thread pool have been executed before printing the results. If not, wait for the tasks to be executed before executing the printing results.

Method 1: isTerminated

We can use the termination status of the thread pool (TERMINATED) to determine whether the tasks of the thread pool have been fully executed, but if the status of the thread pool changes, we need to call the shutdown method of the thread pool, otherwise the thread pool will always be in RUNNING Running state, then there is no way to use the termination state to determine whether the task has been fully executed. Its implementation code is as follows:

import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池任务执行完成判断
 */
public class ThreadPoolCompleted {
    public static void main(String[] args) {
        // 1.创建线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
                0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024));
        // 2.添加任务
        addTask(threadPool);
        // 3.判断线程池是否执行完
        isCompleted(threadPool); // 【核心调用方法】
        // 4.线程池执行完
        System.out.println();
        System.out.println("线程池任务执行完成!");
    }

    /**
     * 方法1:isTerminated 实现方式
     * 判断线程池的所有任务是否执行完
     */
    private static void isCompleted(ThreadPoolExecutor threadPool) {
        threadPool.shutdown();
        while (!threadPool.isTerminated()) { // 如果没有执行完就一直循环
        }
    }

    /**
     * 给线程池添加任务
     */
    private static void addTask(ThreadPoolExecutor threadPool) {
        // 任务总数
        final int taskCount = 5;
        // 添加任务
        for (int i = 0; i < taskCount; i++) {
            final int finalI = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 随机休眠 0-4s
                        int sleepTime = new Random().nextInt(5);
                        TimeUnit.SECONDS.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(String.format("任务%d执行完成", finalI));
                }
            });
        }
    }
}
复制代码

Method description: The shutdown method is a method to initiate an orderly shutdown of the thread pool. It will execute all previously submitted tasks before it is completely shut down, and will not accept any new tasks. When all tasks in the thread pool are executed, the thread pool enters the terminated state, and the result returned by calling the isTerminated method is true.

The execution result of the above program is as follows:image.png

缺点分析

需要关闭线程池。

扩展:线程池的所有状态

线程池总共包含以下 5 种状态:

  • RUNNING:运行状态。
  • SHUTDOWN:关闭状态。
  • STOP:阻断状态。
  • TIDYING:整理状态。
  • TERMINATED:终止状态。

image.png 如果不调用线程池的关闭方法,那么线程池会一直处于 RUNNING 运行状态。

方法2:getCompletedTaskCount

我们可以通过判断线程池中的计划执行任务数和已完成任务数,来判断线程池是否已经全部执行完,如果计划执行任务数=已完成任务数,那么线程池的任务就全部执行完了,否则就未执行完,具体实现代码如下:

/**
 * 方法2:getCompletedTaskCount 实现方式
 * 判断线程池的所有任务是否执行完
 */
private static void isCompletedByTaskCount(ThreadPoolExecutor threadPool) {
    while (threadPool.getTaskCount() != threadPool.getCompletedTaskCount()) {
    }
}
复制代码

以上程序执行结果如下: image.png

方法说明

  • getTaskCount():返回计划执行的任务总数。由于任务和线程的状态可能在计算过程中动态变化,因此返回的值只是一个近似值。
  • getCompletedTaskCount():返回完成执行任务的总数。因为任务和线程的状态可能在计算过程中动态地改变,所以返回的值只是一个近似值,但是在连续的调用中并不会减少。

优缺点分析

此实现方法的优点是无需关闭线程池。 它的缺点是 getTaskCount() 和 getCompletedTaskCount() 返回的是一个近似值,因为线程池中的任务和线程的状态可能在计算过程中动态变化,所以它们两个返回的都是一个近似值。

方法3:CountDownLatch

CountDownLatch 可以理解为一个计数器,我们创建了一个包含 N 个任务的计数器,每个任务执行完计数器 -1,直到计数器减为 0 时,说明所有的任务都执行完了,就可以执行下一段业务的代码了,它的实现流程如下图所示: 具体实现代码如下:

public static void main(String[] args) throws InterruptedException {
    // 创建线程池
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
    	0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024));
    final int taskCount = 5;    // 任务总数
    // 单次计数器
    CountDownLatch countDownLatch = new CountDownLatch(taskCount); // ①
    // 添加任务
    for (int i = 0; i < taskCount; i++) {
        final int finalI = i;
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    // 随机休眠 0-4s
                    int sleepTime = new Random().nextInt(5);
                    TimeUnit.SECONDS.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(String.format("任务%d执行完成", finalI));
                // 线程执行完,计数器 -1
                countDownLatch.countDown();  // ②
            }
        });
    }
    // 阻塞等待线程池任务执行完
    countDownLatch.await();  // ③
    // 线程池执行完
    System.out.println();
    System.out.println("线程池任务执行完成!");
}
复制代码

代码说明:以上代码中标识为 ①、②、③ 的代码行是核心实现代码,其中: ① 是声明一个包含了 5 个任务的计数器; ② 是每个任务执行完之后计数器 -1; ③ 是阻塞等待计数器 CountDownLatch 减为 0,表示任务都执行完了,可以执行 await 方法后面的业务代码了。

以上程序的执行结果如下: image.png

优缺点分析

CountDownLatch 写法很优雅,且无需关闭线程池,但它的缺点是只能使用一次,CountDownLatch 创建之后不能被重复使用,也就是说 CountDownLatch 可以理解为只能使用一次的计数器。

方法4:CyclicBarrier

CyclicBarrier 和 CountDownLatch 类似,它可以理解为一个可以重复使用的循环计数器,CyclicBarrier 可以调用 reset 方法将自己重置到初始状态,CyclicBarrier 具体实现代码如下:

public static void main(String[] args) throws InterruptedException {
    // 创建线程池
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
    	0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024));
    final int taskCount = 5;    // 任务总数
    // 循环计数器 ①
    CyclicBarrier cyclicBarrier = new CyclicBarrier(taskCount, new Runnable() {
        @Override
        public void run() {
            // 线程池执行完
            System.out.println();
            System.out.println("线程池所有任务已执行完!");
        }
    });
    // 添加任务
    for (int i = 0; i < taskCount; i++) {
        final int finalI = i;
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    // 随机休眠 0-4s
                    int sleepTime = new Random().nextInt(5);
                    TimeUnit.SECONDS.sleep(sleepTime);
                    System.out.println(String.format("任务%d执行完成", finalI));
                    // 线程执行完
                    cyclicBarrier.await(); // ②
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
复制代码

以上程序的执行结果如下: image.png

方法说明

CyclicBarrier 有 3 个重要的方法:

  1. 构造方法:构造方法可以传递两个参数,参数 1 是计数器的数量 parties,参数 2 是计数器为 0 时,也就是任务都执行完之后可以执行的事件(方法)。
  2. await 方法:在 CyclicBarrier 上进行阻塞等待,当调用此方法时 CyclicBarrier 的内部计数器会 -1,直到发生以下情形之一:
    1. 在 CyclicBarrier 上等待的线程数量达到 parties,也就是计数器的声明数量时,则所有线程被释放,继续执行。
    2. 当前线程被中断,则抛出 InterruptedException 异常,并停止等待,继续执行。
    3. 其他等待的线程被中断,则当前线程抛出 BrokenBarrierException 异常,并停止等待,继续执行。
    4. 其他等待的线程超时,则当前线程抛出 BrokenBarrierException 异常,并停止等待,继续执行。
    5. 其他线程调用 CyclicBarrier.reset() 方法,则当前线程抛出 BrokenBarrierException 异常,并停止等待,继续执行。
  3. reset 方法:使得CyclicBarrier回归初始状态,直观来看它做了两件事:
    1. 如果有正在等待的线程,则会抛出 BrokenBarrierException 异常,且这些线程停止等待,继续执行。
    2. 将是否破损标志位 broken 置为 false。

优缺点分析

CyclicBarrier 从设计的复杂度到使用的复杂度都高于 CountDownLatch,相比于 CountDownLatch 来说它的优点是可以重复使用(只需调用 reset 就能恢复到初始状态),缺点是使用难度较高。

总结

我们本文提供 4 种判断线程池任务是否执行完的方法:

  1. 使用 isTerminated 方法判断:通过判断线程池的完成状态来实现,需要关闭线程池,一般情况下不建议使用。
  2. 使用 getCompletedTaskCount 方法判断:通过计划执行总任务量和已经完成总任务量,来判断线程池的任务是否已经全部执行,如果相等则判定为全部执行完成。但因为线程个体和状态都会发生改变,所以得到的是一个大致的值,可能不准确。
  3. 使用 CountDownLatch 判断:相当于一个线程安全的单次计数器,使用比较简单,且不需要关闭线程池,是比较常用的判断方法
  4. 使用 CyclicBarrier 判断:相当于一个线程安全的重复计数器,但使用较为复杂,所以日常项目中使用的较少。

是非审之于己,毁誉听之于人,得失安之于数。

公众号:Java面试真题解析

面试合集:gitee.com/mydb/interv…

Guess you like

Origin juejin.im/post/7080694843982217253