From 47d0046742cb747053c34374cf99a6469df2f370 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 13 Aug 2021 16:42:51 -0400 Subject: [PATCH] Add and use ThreadUtils.sleepQuietly(long). --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/ThreadUtils.java | 17 +++++++++++++++++ .../lang3/concurrent/TimedSemaphoreTest.java | 3 ++- .../commons/lang3/time/StopWatchTest.java | 4 ++-- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 77c760276..2c0a330ac 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -63,6 +63,7 @@ The type attribute can be add,update,fix,remove. Add and use ArrayUtils.newInstance(Class>T>, int). Add and use null-safe Streams.of(T...). Add ClassUtils.comparator(). + Add and use ThreadUtils.sleepQuietly(long). Bump spotbugs-maven-plugin from 4.2.0 to 4.2.3 #735. Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764. diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java index 2a5f35d5c..beb4e7553 100644 --- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java +++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java @@ -455,6 +455,23 @@ public class ThreadUtils { DurationUtils.accept(Thread::sleep, duration); } + /** + * Sleeps for the given amount of milliseconds while ignoring {@link InterruptedException}. + *

+ * The sleep duration may be shorter than {@code millis} if we catch a {@link InterruptedException}. + *

+ * + * @param millis the length of time to sleep in milliseconds + * @since 3.13.0 + */ + public static void sleepQuietly(final long millis) { + try { + sleep(Duration.ofMillis(millis)); + } catch (InterruptedException e) { + // be quiet. + } + } + /** *

* ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as diff --git a/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java b/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java index d263abad1..aa550872e 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java @@ -28,6 +28,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.ThreadUtils; import org.easymock.EasyMock; import org.junit.jupiter.api.Test; @@ -99,7 +100,7 @@ public class TimedSemaphoreTest { UNIT, LIMIT); final ScheduledFuture future = semaphore.startTimer(); assertNotNull(future, "No future returned"); - Thread.sleep(PERIOD); + ThreadUtils.sleepQuietly(PERIOD); final int trials = 10; int count = 0; do { diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java index 290be10cd..b59ee8cb3 100644 --- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java +++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java @@ -143,7 +143,7 @@ public class StopWatchTest { @Test public void testFormatSplitTime() throws InterruptedException { final StopWatch watch = StopWatch.createStarted(); - Thread.sleep(MIN_SLEEP_MILLISECONDS); + ThreadUtils.sleepQuietly(MIN_SLEEP_MILLISECONDS); watch.split(); final String formatSplitTime = watch.formatSplitTime(); assertNotEquals(ZERO_TIME_ELAPSED, formatSplitTime); @@ -154,7 +154,7 @@ public class StopWatchTest { public void testFormatSplitTimeWithMessage() throws InterruptedException { final StopWatch watch = new StopWatch(MESSAGE); watch.start(); - Thread.sleep(MIN_SLEEP_MILLISECONDS); + ThreadUtils.sleepQuietly(MIN_SLEEP_MILLISECONDS); watch.split(); final String formatSplitTime = watch.formatSplitTime(); assertFalse(formatSplitTime.startsWith(MESSAGE), formatSplitTime);