Skip to content
Advertisement

future.cancel does not work

I have a nice and compact code, which does not work as I expected.

JavaScript

The output is :

Timeout true

END

Question: Why does not terminate the future.cancel(true) method the called Runnable? After the program wrote the “END” to the output, the “r” Runnable is still running.

Advertisement

Answer

The problem is that your Runnable is not interruptible: task interruption is a collaborative process in Java and the cancelled code needs to check regularly if it’s been cancelled, otherwise it won’t respond to the interruption.

You can amend you code as follows and it should work as expected:

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