Interrupt Handlers

Alan Bateman Alan.Bateman at oracle.com
Tue Dec 21 11:02:35 UTC 2021


On 21/12/2021 00:41, Eric Kolotyluk wrote:
> :
>
> Maybe not the best example in the world, because it's a unit test, but it
> can lead to less boilerplate. I have seen this pattern in other frameworks,
> but dealing with StructuredExecturor CompletionHandlers inspired me to use
> it in my own APIs. It would be nice to see this pattern adopted in other
> Java APIs. For example, I would like to be able to write
>
> Runnable task3 = () -> {
>      for (int i = 0; i < 10; i++) {
>          if (Thread.interrupted()) break;
>          else Thread.sleep(Duration.ofMillis(10), cause -> {});
>      }
> };
>
> Is there an existing JEP where I can make this feature request?
>

Is your issue with Thread.sleep throwing an exception or a more general 
need to invoke cleanup actions when someone is trying to cancel?

Thread.sleep is usually found in test code (like your example). When 
testing then it's normal for a test runner to interrupt threads when the 
test timeout has been reached and it wants the (failed) test to finish. 
Your example seems to be something else, maybe it's a polling task that 
uses Thread.sleep with a short duration to avoid consuming cycles?

As regards cleanup actions then it is something that was partially 
explored in the initial prototypes of SC in 2018. The motivation there 
wasn't "boileplate", instead it was allow cleanup actions to run without 
concern the cleanup action would itself be cancelled/interrupted. It's 
an area that we may look at it again.

-Alan


More information about the loom-dev mailing list