Sleeping and Interruptions
Often, you'll see code like the example below
1 2 3 4 5 |
|
tempus-fugit captures the annoying boiler plate code needed to reset the interrupt flat in situations where you can't or don't want to rethrow the InterruptedException
.
Using the ThreadUtils.sleep method
, the above code is rewritten as.
1
|
|
This ensures that the interrupt flag is reset and is more explicit about the duration of the sleep.
If you want to ensure the interrupt flag is reset for other code, you can use the ThreadUtils.resetInterruptFlagWhen
method directly. The Interruptible
interface is used to highlight that the lamda-like call you want to execute does in fact throw the InterruptedException
. For example;
1 2 3 4 5 6 |
|
Extracting the lamda-like Interruptible
to a method makes the code more expressive;
1
|
|
This is actually how the ThreadUtils.sleep
method is implemented within tempus-fugit.