1616 */
1717package 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>
4042class 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 {
0 commit comments