Add and use ThreadUtils.sleepQuietly(long).

This commit is contained in:
Gary Gregory 2021-08-13 16:42:51 -04:00
parent f7fedbfc54
commit 47d0046742
4 changed files with 22 additions and 3 deletions

View File

@ -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>

View File

@ -455,6 +455,23 @@ public class ThreadUtils {
DurationUtils.accept(Thread::sleep, duration);
}
/**
* 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

View File

@ -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 {

View File

@ -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);