线程-无法在ExecutorService.isShutDown上循环时退出

时间:2019-04-19 20:36:18

标签: java

所以我的线程按预期工作,我只是想在作业中添加一些额外的调味料。

我做了一个while循环,检查是否使用isShutdown,除非shutdown(),否则返回false。已被调用。

所以我在方法末尾调用shutdown,但是它永远不会退出while循环。


    public void runParrallel() throws InterruptedException {
        System.out.println("Submitting Task ...");

        ExecutorService executor = Executors.newFixedThreadPool(5);

        List<Future<TagCounter>> counters = new ArrayList();

        counters.add(executor.submit(new TagCounterCallable("https//www.fck.dk")));
        counters.add(executor.submit(new TagCounterCallable("https://www.google.com")));
        counters.add(executor.submit(new TagCounterCallable("https://politiken.dk")));
        counters.add(executor.submit(new TagCounterCallable("https://cphbusiness.dk")));

        System.out.println("Task is submitted");

        while (!executor.isShutdown()) {
            System.out.println("Task is not completed yet....");
            Thread.sleep(1000);
        }

        for (Future<TagCounter> future : counters) {
            try {
                TagCounter tc = future.get();
                System.out.println("Title: " + tc.getTitle());
                System.out.println("Div's: " + tc.getDivCount());
                System.out.println("Body's: " + tc.getBodyCount());
                System.out.println("----------------------------------");
            } catch (ExecutionException ex) {
                System.out.println("Exception: " + ex);
            }
        }

        executor.shutdown();
    }

3 个答案:

答案 0 :(得分:0)

while循环在您调用shutdown()之前。条件可能无法评估为false,因此您陷入了无限循环。我建议在调用shutdown()之后将while循环移至该点。 另请参见this question,以了解如何关闭ExecutorService。

答案 1 :(得分:0)

如果我错了,请纠正我,但是您似乎要等到提交给ExecutorService的所有任务都完成之后。如果您知道它们将及时完成,则可以将ExecutorService#shutdownExecutorService#awaitTermination结合使用来阻塞执行线程,直到所有任务完成。

这可以通过以下操作完成:

public void runParrallel() throws InterruptedException {
    // Same code to submit tasks.        

    System.out.println("Task is submitted");

    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.DAYS);

    // At this point, the ExecutorService has been shut down successfully 
    // and all tasks have finished.

    for (Future<TagCounter> future : counters) {
        try {
            TagCounter tc = future.get();
            System.out.println("Title: " + tc.getTitle());
            System.out.println("Div's: " + tc.getDivCount());
            System.out.println("Body's: " + tc.getBodyCount());
            System.out.println("----------------------------------");
        } catch (ExecutionException ex) {
            System.out.println("Exception: " + ex);
        }
    }
}

使用此解决方案,可以删除while循环。

答案 2 :(得分:0)

您的while循环无限运行,因为没有任何东西可以激活while循环内的executor.shutdown()。代码不会继续到您调用executor.shutdown()的末尾,因为while循环的条件会返回到while循环的开始。

在while循环中放入一个if语句。 if语句检查是否已提交任务,如果已提交,则将调用executor.shutdown()

以下仅是示例:

while (!executor.isShutdown()) {
        System.out.println("Task is not completed yet....");
        Thread.sleep(1000);
        if(TaskIsCompleted){
          executor.shutdown();
        }
    }