You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/09/07 20:07:03 UTC

[commons-io] branch master updated: ThreadMonitor.sleep(Duration) ignores nanoseconds.

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


The following commit(s) were added to refs/heads/master by this push:
     new 18a2406  ThreadMonitor.sleep(Duration) ignores nanoseconds.
18a2406 is described below

commit 18a24066db1c1e9f71299e9ab5e6b1b193f77720
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Sep 7 16:07:00 2021 -0400

    ThreadMonitor.sleep(Duration) ignores nanoseconds.
---
 src/changes/changes.xml                               |  3 +++
 .../java/org/apache/commons/io/ThreadMonitor.java     | 19 ++++++++++---------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d32d7f6..a4450a2 100644
--- a/src/changes/changes.xml
+++ b/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.
diff --git a/src/main/java/org/apache/commons/io/ThreadMonitor.java b/src/main/java/org/apache/commons/io/ThreadMonitor.java
index 0f0c989..3dad373 100644
--- a/src/main/java/org/apache/commons/io/ThreadMonitor.java
+++ b/src/main/java/org/apache/commons/io/ThreadMonitor.java
@@ -17,6 +17,7 @@
 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 @@ class ThreadMonitor implements Runnable {
      * 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