File tree Expand file tree Collapse file tree
main/java/org/apache/commons/lang3
test/java/org/apache/commons/lang3 Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -519,7 +519,8 @@ public static void sleepQuietly(final Duration duration) {
519519 try {
520520 sleep (duration );
521521 } catch (final InterruptedException ignore ) {
522- // Ignore & be quiet.
522+ // Restore interrupted state, ignore & be quiet.
523+ Thread .currentThread ().interrupt ();
523524 }
524525 }
525526
Original file line number Diff line number Diff line change @@ -345,7 +345,11 @@ public synchronized boolean isInitialized() {
345345 try {
346346 future .get ();
347347 return true ;
348- } catch (CancellationException | ExecutionException | InterruptedException e ) {
348+ } catch (CancellationException | ExecutionException e ) {
349+ return false ;
350+ } catch (InterruptedException e ) {
351+ // reset interrupted state
352+ Thread .currentThread ().interrupt ();
349353 return false ;
350354 }
351355 }
Original file line number Diff line number Diff line change 3636import java .util .List ;
3737import java .util .Objects ;
3838import java .util .concurrent .CountDownLatch ;
39+ import java .util .concurrent .TimeUnit ;
40+ import java .util .concurrent .atomic .AtomicBoolean ;
41+ import java .util .concurrent .atomic .AtomicReference ;
3942import java .util .function .Predicate ;
4043
4144import org .apache .commons .lang3 .ThreadUtils .ThreadGroupPredicate ;
@@ -421,4 +424,26 @@ void testThreadsSameName() throws InterruptedException {
421424 }
422425 }
423426
427+
428+ @ Test
429+ void sleepQuietly () throws Exception {
430+ CountDownLatch started = new CountDownLatch (1 );
431+ AtomicBoolean interruptedAfter = new AtomicBoolean (false );
432+ AtomicReference <Throwable > thrown = new AtomicReference <>();
433+ Thread worker = new Thread (() -> {
434+ started .countDown ();
435+ try {
436+ ThreadUtils .sleepQuietly (Duration .ofDays (1 )); // should be cut short by interrupt
437+ interruptedAfter .set (Thread .currentThread ().isInterrupted ());
438+ } catch (Throwable t ) {
439+ thrown .set (t );
440+ }
441+ });
442+ worker .start ();
443+ assertTrue (started .await (2 , TimeUnit .SECONDS ), "Worker did not start in time" );
444+ worker .interrupt ();
445+ worker .join ();
446+ assertNull (thrown .get ());
447+ assertTrue (interruptedAfter .get ());
448+ }
424449}
You can’t perform that action at this time.
0 commit comments