aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/lang3/ThreadUtils.java
diff options
context:
space:
mode:
authorGary Gregory <garydgregory@gmail.com>2021-02-21 18:14:34 -0500
committerGary Gregory <garydgregory@gmail.com>2021-02-21 18:14:34 -0500
commita98a6a99d9415c116087b07f1d777dbe20b889da (patch)
treef9cd3b2dfa63f6958b70b3ed68be73be6c1b3b40 /src/main/java/org/apache/commons/lang3/ThreadUtils.java
parent3c96b101bd04ab1d48c26edfa1ebbc47c02f274c (diff)
downloadapache-commons-lang-a98a6a99d9415c116087b07f1d777dbe20b889da.tar.gz
Add and use Duration in APIs.
- Add and use ThreadUtils.sleep(Duration). - Add and use ThreadUtils.join(Thread, Duration). - Add ObjectUtils.wait(Duration).
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/ThreadUtils.java')
-rw-r--r--src/main/java/org/apache/commons/lang3/ThreadUtils.java28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java
index 5fffd9443..2a5f35d5c 100644
--- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java
@@ -16,11 +16,14 @@
*/
package org.apache.commons.lang3;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import org.apache.commons.lang3.time.DurationUtils;
+
/**
* <p>
* Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
@@ -428,6 +431,31 @@ public class ThreadUtils {
}
/**
+ * Waits for the given thread to die for the given duration. Implemented using {@link Thread#join(long, int)}.
+ *
+ * @param thread The thread to join.
+ * @param duration How long to wait.
+ * @throws InterruptedException if any thread has interrupted the current thread.
+ * @see Thread#join(long, int)
+ * @since 3.12.0
+ */
+ public static void join(final Thread thread, final Duration duration) throws InterruptedException {
+ DurationUtils.accept(thread::join, duration);
+ }
+
+ /**
+ * Sleeps the current thread for the given duration. Implemented using {@link Thread#sleep(long, int)}.
+ *
+ * @param duration How long to sleep.
+ * @throws InterruptedException if any thread has interrupted the current thread.
+ * @see Thread#sleep(long, int)
+ * @since 3.12.0
+ */
+ public static void sleep(final Duration duration) throws InterruptedException {
+ DurationUtils.accept(Thread::sleep, duration);
+ }
+
+ /**
* <p>
* ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
* {@code ThreadUtils.getAllThreads()}