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-io.git
commit daa5d8cd8bb18327d84fe8815b842a57ce3c3f5b Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Feb 14 11:09:32 2021 -0500 Re-implement package-private class ThreadMonitor timeouts with java.time.Duration. --- .../org/apache/commons/io/FileSystemUtils.java | 3 +- .../java/org/apache/commons/io/ThreadMonitor.java | 34 ++++++++++++---------- .../apache/commons/io/ThreadMonitorTestCase.java | 10 ++++--- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileSystemUtils.java b/src/main/java/org/apache/commons/io/FileSystemUtils.java index f957e45..ca97888 100644 --- a/src/main/java/org/apache/commons/io/FileSystemUtils.java +++ b/src/main/java/org/apache/commons/io/FileSystemUtils.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -495,7 +496,7 @@ public class FileSystemUtils { BufferedReader inr = null; try { - final Thread monitor = ThreadMonitor.start(timeout); + final Thread monitor = ThreadMonitor.start(Duration.ofMillis(timeout)); proc = openProcess(cmdAttribs); in = proc.getInputStream(); diff --git a/src/main/java/org/apache/commons/io/ThreadMonitor.java b/src/main/java/org/apache/commons/io/ThreadMonitor.java index dc7386e..dee55b9 100644 --- a/src/main/java/org/apache/commons/io/ThreadMonitor.java +++ b/src/main/java/org/apache/commons/io/ThreadMonitor.java @@ -16,6 +16,8 @@ */ package org.apache.commons.io; +import java.time.Duration; + /** * Monitors a thread, interrupting it if it reaches the specified timeout. * <p> @@ -40,7 +42,7 @@ package org.apache.commons.io; class ThreadMonitor implements Runnable { private final Thread thread; - private final long timeout; + private final Duration timeout; /** * Start monitoring the current thread. @@ -50,7 +52,7 @@ class ThreadMonitor implements Runnable { * @return The monitor thread or {@code null} * if the timeout amount is not greater than zero */ - public static Thread start(final long timeout) { + static Thread start(final Duration timeout) { return start(Thread.currentThread(), timeout); } @@ -63,14 +65,14 @@ class ThreadMonitor implements Runnable { * @return The monitor thread or {@code null} * if the timeout amount is not greater than zero */ - public static Thread start(final Thread thread, final long timeout) { - Thread monitor = null; - if (timeout > 0) { - final ThreadMonitor timout = new ThreadMonitor(thread, timeout); - monitor = new Thread(timout, ThreadMonitor.class.getSimpleName()); - monitor.setDaemon(true); - monitor.start(); + static Thread start(final Thread thread, final Duration timeout) { + if (timeout.isZero() || timeout.isNegative()) { + return null; } + final ThreadMonitor timout = new ThreadMonitor(thread, timeout); + final Thread monitor = new Thread(timout, ThreadMonitor.class.getSimpleName()); + monitor.setDaemon(true); + monitor.start(); return monitor; } @@ -79,19 +81,19 @@ class ThreadMonitor implements Runnable { * * @param thread The monitor thread, may be {@code null} */ - public static void stop(final Thread thread) { + static void stop(final Thread thread) { if (thread != null) { thread.interrupt(); } } /** - * Construct and new monitor. + * Constructs a new monitor. * * @param thread The thread to monitor * @param timeout The timeout amount in milliseconds */ - private ThreadMonitor(final Thread thread, final long timeout) { + private ThreadMonitor(final Thread thread, final Duration timeout) { this.thread = thread; this.timeout = timeout; } @@ -113,15 +115,17 @@ class ThreadMonitor implements Runnable { } /** - * Sleep for a guaranteed minimum number of milliseconds unless interrupted. + * Sleeps for a guaranteed minimum duration unless interrupted. * * This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else * it deems appropriate. Read the docs on Thread.sleep for further interesting details. * - * @param millis the number of milliseconds to sleep for + * @param duration the sleep duration. * @throws InterruptedException if interrupted */ - private static void sleep(final long millis) throws InterruptedException { + private static void sleep(final Duration duration) throws InterruptedException { + // Ignore nanos for now. + final long millis = duration.toMillis(); final long finishAtMillis = System.currentTimeMillis() + millis; long remainingMillis = millis; do { diff --git a/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java b/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java index c3fa0c4..e3009e2 100644 --- a/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java +++ b/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java @@ -19,6 +19,8 @@ package org.apache.commons.io; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.fail; +import java.time.Duration; + import org.apache.commons.io.test.TestUtils; import org.junit.jupiter.api.Test; @@ -33,7 +35,7 @@ public class ThreadMonitorTestCase { @Test public void testTimeout() { try { - final Thread monitor = ThreadMonitor.start(100); + final Thread monitor = ThreadMonitor.start(Duration.ofMillis(100)); TestUtils.sleep(200); ThreadMonitor.stop(monitor); fail("Expected InterruptedException"); @@ -48,7 +50,7 @@ public class ThreadMonitorTestCase { @Test public void testCompletedWithoutTimeout() { try { - final Thread monitor = ThreadMonitor.start(200); + final Thread monitor = ThreadMonitor.start(Duration.ofMillis(200)); TestUtils.sleep(100); ThreadMonitor.stop(monitor); } catch (final InterruptedException e) { @@ -64,7 +66,7 @@ public class ThreadMonitorTestCase { // timeout = -1 try { - final Thread monitor = ThreadMonitor.start(-1); + final Thread monitor = ThreadMonitor.start(Duration.ofMillis(-1)); assertNull(monitor,"Timeout -1, Monitor should be null"); TestUtils.sleep(100); ThreadMonitor.stop(monitor); @@ -74,7 +76,7 @@ public class ThreadMonitorTestCase { // timeout = 0 try { - final Thread monitor = ThreadMonitor.start(0); + final Thread monitor = ThreadMonitor.start(Duration.ZERO); assertNull(monitor, "Timeout 0, Monitor should be null"); TestUtils.sleep(100); ThreadMonitor.stop(monitor);