Skip to content

Commit daa5d8c

Browse files
author
Gary Gregory
committed
Re-implement package-private class ThreadMonitor timeouts with
java.time.Duration.
1 parent 5c39717 commit daa5d8c

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

src/main/java/org/apache/commons/io/FileSystemUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStreamReader;
2424
import java.io.OutputStream;
2525
import java.nio.charset.Charset;
26+
import java.time.Duration;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.List;
@@ -495,7 +496,7 @@ List<String> performCommand(final String[] cmdAttribs, final int max, final long
495496
BufferedReader inr = null;
496497
try {
497498

498-
final Thread monitor = ThreadMonitor.start(timeout);
499+
final Thread monitor = ThreadMonitor.start(Duration.ofMillis(timeout));
499500

500501
proc = openProcess(cmdAttribs);
501502
in = proc.getInputStream();

src/main/java/org/apache/commons/io/ThreadMonitor.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.commons.io;
1818

19+
import java.time.Duration;
20+
1921
/**
2022
* Monitors a thread, interrupting it if it reaches the specified timeout.
2123
* <p>
@@ -40,7 +42,7 @@
4042
class ThreadMonitor implements Runnable {
4143

4244
private final Thread thread;
43-
private final long timeout;
45+
private final Duration timeout;
4446

4547
/**
4648
* Start monitoring the current thread.
@@ -50,7 +52,7 @@ class ThreadMonitor implements Runnable {
5052
* @return The monitor thread or {@code null}
5153
* if the timeout amount is not greater than zero
5254
*/
53-
public static Thread start(final long timeout) {
55+
static Thread start(final Duration timeout) {
5456
return start(Thread.currentThread(), timeout);
5557
}
5658

@@ -63,14 +65,14 @@ public static Thread start(final long timeout) {
6365
* @return The monitor thread or {@code null}
6466
* if the timeout amount is not greater than zero
6567
*/
66-
public static Thread start(final Thread thread, final long timeout) {
67-
Thread monitor = null;
68-
if (timeout > 0) {
69-
final ThreadMonitor timout = new ThreadMonitor(thread, timeout);
70-
monitor = new Thread(timout, ThreadMonitor.class.getSimpleName());
71-
monitor.setDaemon(true);
72-
monitor.start();
68+
static Thread start(final Thread thread, final Duration timeout) {
69+
if (timeout.isZero() || timeout.isNegative()) {
70+
return null;
7371
}
72+
final ThreadMonitor timout = new ThreadMonitor(thread, timeout);
73+
final Thread monitor = new Thread(timout, ThreadMonitor.class.getSimpleName());
74+
monitor.setDaemon(true);
75+
monitor.start();
7476
return monitor;
7577
}
7678

@@ -79,19 +81,19 @@ public static Thread start(final Thread thread, final long timeout) {
7981
*
8082
* @param thread The monitor thread, may be {@code null}
8183
*/
82-
public static void stop(final Thread thread) {
84+
static void stop(final Thread thread) {
8385
if (thread != null) {
8486
thread.interrupt();
8587
}
8688
}
8789

8890
/**
89-
* Construct and new monitor.
91+
* Constructs a new monitor.
9092
*
9193
* @param thread The thread to monitor
9294
* @param timeout The timeout amount in milliseconds
9395
*/
94-
private ThreadMonitor(final Thread thread, final long timeout) {
96+
private ThreadMonitor(final Thread thread, final Duration timeout) {
9597
this.thread = thread;
9698
this.timeout = timeout;
9799
}
@@ -113,15 +115,17 @@ public void run() {
113115
}
114116

115117
/**
116-
* Sleep for a guaranteed minimum number of milliseconds unless interrupted.
118+
* Sleeps for a guaranteed minimum duration unless interrupted.
117119
*
118120
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
119121
* it deems appropriate. Read the docs on Thread.sleep for further interesting details.
120122
*
121-
* @param millis the number of milliseconds to sleep for
123+
* @param duration the sleep duration.
122124
* @throws InterruptedException if interrupted
123125
*/
124-
private static void sleep(final long millis) throws InterruptedException {
126+
private static void sleep(final Duration duration) throws InterruptedException {
127+
// Ignore nanos for now.
128+
final long millis = duration.toMillis();
125129
final long finishAtMillis = System.currentTimeMillis() + millis;
126130
long remainingMillis = millis;
127131
do {

src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.junit.jupiter.api.Assertions.assertNull;
2020
import static org.junit.jupiter.api.Assertions.fail;
2121

22+
import java.time.Duration;
23+
2224
import org.apache.commons.io.test.TestUtils;
2325
import org.junit.jupiter.api.Test;
2426

@@ -33,7 +35,7 @@ public class ThreadMonitorTestCase {
3335
@Test
3436
public void testTimeout() {
3537
try {
36-
final Thread monitor = ThreadMonitor.start(100);
38+
final Thread monitor = ThreadMonitor.start(Duration.ofMillis(100));
3739
TestUtils.sleep(200);
3840
ThreadMonitor.stop(monitor);
3941
fail("Expected InterruptedException");
@@ -48,7 +50,7 @@ public void testTimeout() {
4850
@Test
4951
public void testCompletedWithoutTimeout() {
5052
try {
51-
final Thread monitor = ThreadMonitor.start(200);
53+
final Thread monitor = ThreadMonitor.start(Duration.ofMillis(200));
5254
TestUtils.sleep(100);
5355
ThreadMonitor.stop(monitor);
5456
} catch (final InterruptedException e) {
@@ -64,7 +66,7 @@ public void testNoTimeout() {
6466

6567
// timeout = -1
6668
try {
67-
final Thread monitor = ThreadMonitor.start(-1);
69+
final Thread monitor = ThreadMonitor.start(Duration.ofMillis(-1));
6870
assertNull(monitor,"Timeout -1, Monitor should be null");
6971
TestUtils.sleep(100);
7072
ThreadMonitor.stop(monitor);
@@ -74,7 +76,7 @@ public void testNoTimeout() {
7476

7577
// timeout = 0
7678
try {
77-
final Thread monitor = ThreadMonitor.start(0);
79+
final Thread monitor = ThreadMonitor.start(Duration.ZERO);
7880
assertNull(monitor, "Timeout 0, Monitor should be null");
7981
TestUtils.sleep(100);
8082
ThreadMonitor.stop(monitor);

0 commit comments

Comments
 (0)