【并发编程】- 线程池使用poll()方法设置等待时间移除已完成的任务

136 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第19天,点击查看活动详情

接着上一篇文章-【并发编程】- ExecutorCompletionService使用poll()方法移除已完成的任务

ExecutorCompletionService使用poll()方法设置等待时间移除已完成的任务

修改运行类执行代码如下:

public class SecondPollRun {
    public static void main(String[] args) {
        PollFirstCallable pollFirstCallable = new PollFirstCallable();
        PollSecondCallable pollSecondCallable = new PollSecondCallable();

        ExecutorService executorService = Executors.newCachedThreadPool();
        ExecutorCompletionService completionService = new ExecutorCompletionService(executorService);
        completionService.submit(pollFirstCallable);
        completionService.submit(pollSecondCallable);

        for (int i = 0; i < 2; i++) {
            try {
                System.out.println("移除已完成任务的结果:"+completionService.poll(7, TimeUnit.SECONDS).get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println("主线程输出结果完成!");
    }
}

运行结果如下:

com.ozx.concurrentprogram.executor.service.PollFirstCallable线程执行任务时间:1660285569519

移除已完成任务的结果:com.ozx.concurrentprogram.executor.service.PollFirstCallable

com.ozx.concurrentprogram.executor.service.PollSecondCallable线程执行任务时间:1660285574515

移除已完成任务的结果:com.ozx.concurrentprogram.executor.service.PollSecondCallable

主线程输出结果完成!

从运行结果看出一个线程休眠了5秒,poll()方法等待获取返回值的时间是6s,completionService调用poll()方法能移除第一个线程,但是第二个线程休眠时间是10秒,所以再调用poll()方法移除第二个线程,所以一共等待12秒才能将两个已完成的线程移除。

修改运行类代码如下:

public class SecondPollRun {
    public static void main(String[] args) {
        PollFirstCallable pollFirstCallable = new PollFirstCallable();
        PollSecondCallable pollSecondCallable = new PollSecondCallable();

        ExecutorService executorService = Executors.newCachedThreadPool();
        ExecutorCompletionService completionService = new ExecutorCompletionService(executorService);
        completionService.submit(pollFirstCallable);
        completionService.submit(pollSecondCallable);


            try {
                System.out.println("poll等待4秒");
                System.out.println("移除已完成任务的结果:"+completionService.poll(4, TimeUnit.SECONDS));
                System.out.println("----------------");
                System.out.println("poll等待6秒");
                System.out.println("移除已完成任务的结果:"+completionService.poll(6, TimeUnit.SECONDS).get());
                System.out.println("----------------");
                System.out.println("poll等待11秒");
                System.out.println("移除已完成任务的结果:"+completionService.poll(11, TimeUnit.SECONDS).get());
                System.out.println("----------------");

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        System.out.println("主线程输出结果完成!");
    }
}

运行结果如下:

poll等待4秒

移除已完成任务的结果:null

poll等待6秒

com.ozx.concurrentprogram.executor.service.PollFirstCallable线程执行任务时间:1660286411233

移除已完成任务的结果:com.ozx.concurrentprogram.executor.service.PollFirstCallable

poll等待11秒

com.ozx.concurrentprogram.executor.service.PollSecondCallable线程执行任务时间:1660286416225

移除已完成任务的结果:com.ozx.concurrentprogram.executor.service.PollSecondCallable

主线程输出结果完成!

从运行结果看出ExecutorCompletionService调用poll()方法等待时间小于任务执行的时间,那么返回值为null。