Future集合会等线程池执行完才开始遍历吗?

先说结论:Future集合并不是等线程池执行完才开始遍历,而是线程池内的线程执行完一条Future集合就立即遍历一条


在使用线程池的业务场景下,我们经常需要获取线程执行的返回值,此时我们需要Callable对象当做线程池参数并用List<Future>接收,然后遍历List<Future>获取我们想要的值。

但是,如果List<Future>是等线程池中所有的线程执行完后才开始遍历,就意味着List集合可能会非常大,进而导致内存溢出程序崩溃。所以我们就来验证一下List<Future>是会等待线程池执行完毕开始遍历还是只要线程执行完一条就遍历一条

示例:

ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 5, 60L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
List<Future<Integer>> futureList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    int finalI = i;
    Future<Integer> future = executor.submit(() -> {
        Thread.sleep(1000L);
        System.out.println(Thread.currentThread().getName() + " " + finalI);
        return finalI;
    });
    futureList.add(future);
}
System.out.println("futureList.size() = "+futureList.size());
Iterator<Future<Integer>> iterator = futureList.iterator();
while (iterator.hasNext()) {
    Integer i = iterator.next().get();
    iterator.remove();
    System.out.println("执行后 " + i);
    //业务代码......
}

执行结果:

由此足以证明List<Future>集合不会等待线程池执行完才开始遍历,只要我们在遍历后把元素删除就能一定程度避免List<Future>过大导致内存溢出。

注意这里说的是一定程度,并不能完全避免,这取决于“业务代码”的执行时长,如果上面线程池都执行完了,下面一个业务都没处理完,同样会导致List<Future>占用很多内存,所以业务代码尽量放到上面的线程池里面。

当然如果能确定List<Future>不会很大导致内存溢出放到下面遍历自然更加优美,还请根据实际业务情况抉择。

posted @ 2024-06-13 15:05  救苦救难韩天尊  阅读(17)  评论(0编辑  收藏  举报