This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 47d0046  Add and use ThreadUtils.sleepQuietly(long).
47d0046 is described below

commit 47d0046742cb747053c34374cf99a6469df2f370
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Aug 13 16:42:51 2021 -0400

    Add and use ThreadUtils.sleepQuietly(long).
---
 src/changes/changes.xml                                 |  1 +
 src/main/java/org/apache/commons/lang3/ThreadUtils.java | 17 +++++++++++++++++
 .../commons/lang3/concurrent/TimedSemaphoreTest.java    |  3 ++-
 .../org/apache/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 77c7602..2c0a330 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add and use ArrayUtils.newInstance(Class&gt;T>, int).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add and use null-safe Streams.of(T...).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add ClassUtils.comparator().</action>
+    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add and use ThreadUtils.sleepQuietly(long).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, 
Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.3 #735.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot, 
XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, 
#764.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java 
b/src/main/java/org/apache/commons/lang3/ThreadUtils.java
index 2a5f35d..beb4e75 100644
--- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java
@@ -456,6 +456,23 @@ public class ThreadUtils {
     }
 
     /**
+     * Sleeps for the given amount of milliseconds while ignoring {@link 
InterruptedException}.
+     * <p>
+     * The sleep duration may be shorter than {@code millis} if we catch a 
{@link InterruptedException}.
+     * </p>
+     *
+     * @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.
+        }
+    }
+
+    /**
      * <p>
      * ThreadUtils instances should NOT be constructed in standard 
programming. Instead, the class should be used as
      * {@code ThreadUtils.getAllThreads()}
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 d263aba..aa55087 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 290be10..b59ee8c 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);

Reply via email to