Re-implement package-private class ThreadMonitor timeouts with
java.time.Duration.
MODIFY
src/main/java/org/apache/commons/io/FileSystemUtils.java
src/main/java/org/apache/commons/io/FileSystemUtils.java
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 @@ long parseBytes(final String freeSpace, final String path) throws IOException {
BufferedReader inr = null;
try {
- final Thread monitor = ThreadMonitor.start(timeout);
+ final Thread monitor = ThreadMonitor.start(Duration.ofMillis(timeout));
proc = openProcess(cmdAttribs);
in = proc.getInputStream();

MODIFY
src/main/java/org/apache/commons/io/ThreadMonitor.java
src/main/java/org/apache/commons/io/ThreadMonitor.java
*/
package org.apache.commons.io;
+import java.time.Duration;
+
/**
* Monitors a thread, interrupting it if it reaches the specified timeout.
* <p>
class ThreadMonitor implements Runnable {
private final Thread thread;
- private final long timeout;
+ private final Duration timeout;
/**
* Start monitoring the current thread.
* @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 @@ public static Thread start(final long timeout) {
* @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) {
+ static Thread start(final Thread thread, final Duration timeout) {
+ if (timeout.isZero() || timeout.isNegative()) {
+ return null;
+ }
final ThreadMonitor timout = new ThreadMonitor(thread, timeout);
- monitor = new Thread(timout, ThreadMonitor.class.getSimpleName());
+ final Thread monitor = new Thread(timout, ThreadMonitor.class.getSimpleName());
monitor.setDaemon(true);
monitor.start();
- }
return monitor;
}
@@ -79,19 +81,19 @@ public static Thread start(final Thread thread, final long timeout) {
*
* @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 @@ public void run() {
}
/**
- * 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 {

MODIFY
src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java
src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java
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;
@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 void testTimeout() {
@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 void testNoTimeout() {
// 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 void testNoTimeout() {
// 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);

Re-implement package-private class ThreadMonitor timeouts with
java.time.Duration.