Skip to content
Advertisement

Java concurrent programming – endless loop

I am reading the book Introducing Play Framework: Java Web Application Development (ISBN 978-1-4842-5645-9) and there is this example on Callable:

JavaScript

My question is, if the computation of the Future throws and exception, will the while loop run forever?


In my opinion, yes, it will loop forever.

First, if the Future computation throws an exception, then

  • future.isDone()

always evaluates to true. So we reach the inside of the if, where the stopping condition can be set. Still Ok.

Second, as per future documentation, when reaching line

  • result = future.get();

it will always throw ExecutionException because the computation threw an exception. This exception is caught in the try-catch block, without reaching the stopping condition precondition, which is

  • listen = false

Lastly, the above will create an endless cycle.

Is there any mistake in my assumptions or is the author of the example really wrong?

Advertisement

Answer

The program mentioned above cycles forever, if the callable throws an exception.


This is a code snippet with the callable that throws an exceptions. Executing compiled snippet loops forever.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement