ThreadMonitor.sleep(Duration) ignores nanoseconds.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -68,6 +68,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-748" dev="ggregory" type="fix" due-to="Dirk Heinrichs, Gary Gregory">
FileUtils.moveToDirectory() exception documentation and exception message error.
</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ ThreadMonitor.sleep(Duration) ignores nanoseconds.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BrokenReader.INSTANCE.

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;
+import java.time.Instant;
/**
* Monitors a thread, interrupting it if it reaches the specified timeout.
@@ -118,21 +119,21 @@ public void run() {
* 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.
+ * it deems appropriate. Read {@link Thread#sleep()} for further interesting details.
*
* @param duration the sleep duration.
* @throws InterruptedException if interrupted
*/
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;
+ final Instant finishInstant = Instant.now().plus(duration);
+ Duration remainingDuration = duration;
do {
- Thread.sleep(remainingMillis);
- remainingMillis = finishAtMillis - System.currentTimeMillis();
- } while (remainingMillis > 0);
+ Thread.sleep(remainingDuration.toMillis(), getNanosOfMiili(remainingDuration));
+ remainingDuration = Duration.between(Instant.now(), finishInstant);
+ } while (!remainingDuration.isNegative());
}
-
+ private static int getNanosOfMiili(final Duration duration) {
+ return duration.getNano() % 1_000_000;
+ }
}
\ No newline at end of file

ThreadMonitor.sleep(Duration) ignores nanoseconds.