0

So my threads are working as expected, and I just wanted to add some extra sauce to my homework.

I made a while loop that checks uses the isShutdown which returns false unless shutdown(); has been called.

So i call shutdown at the end of my method, but it won't ever exit the while loop.


    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();
    }

dumbprogrammer
  • 351
  • 1
  • 3
  • 14
  • By the way, [ExecutorService already has a method](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/concurrent/ExecutorService.html#awaitTermination%28long,java.util.concurrent.TimeUnit%29) for doing that. But, as others said, you first have to call executor.shutdown(). – VGR Apr 19 '19 at 22:45

3 Answers3

0

The while-loop is before you ever call shutdown(). The condition cannot possibly evaluate to false, thus you are stuck with an infinite loop. I'd suggest moving the while loop to the point after you call shutdown(). See also this question on how to shut down an ExecutorService.

tronski
  • 90
  • 1
  • 8
0

Correct me if I'm wrong, but it looks like you want to wait until all tasks that were submitted to your ExecutorService have finished. If you know that they're going to finish in a timely manner, then you can use ExecutorService#shutdown in conjunction with ExecutorService#awaitTermination to block the executing thread until all tasks are complete.

This can be done with the following:

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);
        }
    }
}

With this solution, the while loop can be removed.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
0

Your while-loop is running infinitely because there is nothing that activates the executor.shutdown() inside the while-loop. The code wont progress to the end where you call executor.shutdown() because the while-loop's condition returns back to the start of the while-loop.

Put an if-statement inside the while-loop. The if-statement checks if the task is submitted, and if it is, the executor.shutdown() will be called.

Following is just an example:

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