/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.io;

import java.time.Duration;
import java.time.Instant;

/**
 * Monitors a thread, interrupting it if it reaches the specified timeout.
 * <p>
 * This works by sleeping until the specified timeout amount and then
 * interrupting the thread being monitored. If the thread being monitored
 * completes its work before being interrupted, it should {@code interrupt()}
 * the <i>monitor</i> thread.
 * </p>
 *
 * <pre>
 *       Duration timeout = Duration.ofSeconds(1);
 *       try {
 *           Thread monitor = ThreadMonitor.start(timeout);
 *           // do some work here
 *           ThreadMonitor.stop(monitor);
 *       } catch (InterruptedException e) {
 *           // timed amount was reached
 *       }
 * </pre>
 *
 */
class ThreadMonitor implements Runnable {

    private final Thread thread;
    private final Duration timeout;

    /**
     * Start monitoring the current thread.
     *
     * @param timeout The timeout amount.
     * or no timeout if the value is zero or less
     * @return The monitor thread or {@code null}
     * if the timeout amount is not greater than zero
     */
    static Thread start(final Duration timeout) {
        return start(Thread.currentThread(), timeout);
    }

    /**
     * Start monitoring the specified thread.
     *
     * @param thread The thread The thread to monitor
     * @param timeout The timeout amount.
     * or no timeout if the value is zero or less
     * @return The monitor thread or {@code null}
     * if the timeout amount is not greater than zero
     */
    static Thread start(final Thread thread, final Duration timeout) {
        if (timeout.isZero() || timeout.isNegative()) {
            return null;
        }
        final ThreadMonitor timout = new ThreadMonitor(thread, timeout);
        final Thread monitor = new Thread(timout, ThreadMonitor.class.getSimpleName());
        monitor.setDaemon(true);
        monitor.start();
        return monitor;
    }

    /**
     * Stop monitoring the specified thread.
     *
     * @param thread The monitor thread, may be {@code null}
     */
    static void stop(final Thread thread) {
        if (thread != null) {
            thread.interrupt();
        }
    }

    /**
     * Constructs a new monitor.
     *
     * @param thread The thread to monitor.
     * @param timeout The timeout amount.
     */
    private ThreadMonitor(final Thread thread, final Duration timeout) {
        this.thread = thread;
        this.timeout = timeout;
    }

    /**
     * Sleep until the specified timeout amount and then
     * interrupt the thread being monitored.
     *
     * @see Runnable#run()
     */
    @Override
    public void run() {
        try {
            sleep(timeout);
            thread.interrupt();
        } catch (final InterruptedException e) {
            // timeout not reached
        }
    }

    /**
     * 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 {@link Thread#sleep(long, int)}} for further interesting details.
     *
     * @param duration the sleep duration.
     * @throws InterruptedException if interrupted
     */
    private static void sleep(final Duration duration) throws InterruptedException {
        final Instant finishInstant = Instant.now().plus(duration);
        Duration remainingDuration = duration;
        do {
            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;
    }
}
@@ -1037,19 +1037,16 @@
 and then
-%0A *
  interru
@@ -1081,24 +1081,27 @@
 ored. If the
+%0A *
  thread bein
@@ -1111,19 +1111,16 @@
 onitored
-%0A *
  complet
@@ -1186,19 +1186,16 @@
 rrupt()%7D
-%0A *
  the %3Ci%3E
@@ -1205,16 +1205,19 @@
 itor%3C/i%3E
+%0A *
  thread.
@@ -1239,22 +1239,16 @@
 %3Cpre%3E%0A *
-      
  Duratio
@@ -1284,22 +1284,16 @@
 s(1);%0A *
-      
  try %7B%0A 
@@ -1294,30 +1294,24 @@
 ry %7B%0A *     
-   
-   
 Thread monit
@@ -1347,22 +1347,16 @@
 out);%0A *
-      
      // 
@@ -1375,22 +1375,16 @@
  here%0A *
-      
      Thr
@@ -1411,22 +1411,16 @@
 tor);%0A *
-      
  %7D catch
@@ -1449,22 +1449,16 @@
  e) %7B%0A *
-      
      // 
@@ -1484,22 +1484,16 @@
 ached%0A *
-      
  %7D%0A * %3C/
@@ -1709,39 +1709,32 @@
  timeout amount.
-%0A     *
  or no timeout i
@@ -1752,32 +1752,33 @@
  is zero or less
+.
 %0A     * @return 
@@ -1803,39 +1803,32 @@
  or %7B@code null%7D
-%0A     *
  if the timeout 
@@ -1850,32 +1850,33 @@
 reater than zero
+.
 %0A     */%0A    sta
@@ -2136,23 +2136,16 @@
  amount.
-%0A     *
  or no t
@@ -2179,16 +2179,17 @@
  or less
+.
 %0A     * 
@@ -2230,23 +2230,16 @@
 de null%7D
-%0A     *
  if the 
@@ -2277,16 +2277,17 @@
 han zero
+.
 %0A     */
@@ -3299,23 +3299,16 @@
 and then
-%0A     *
  interru
@@ -3778,23 +3778,16 @@
 ing else
-%0A     *
  it deem
@@ -3800,16 +3800,23 @@
 opriate.
+%0A     *
  Read %7B@
@@ -4551,8 +4551,9 @@
 %0A    %7D%0A%7D
+%0A

Format for max line length.