ThreadUtils.java

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

  18. import java.time.Duration;
  19. import java.time.Instant;

  20. /**
  21.  * Helps work with threads.
  22.  *
  23.  * @since 2.12.0
  24.  */
  25. public final class ThreadUtils {

  26.     private static int getNanosOfMilli(final Duration duration) {
  27.         return duration.getNano() % 1_000_000;
  28.     }

  29.     /**
  30.      * Sleeps for a guaranteed minimum duration unless interrupted.
  31.      * <p>
  32.      * This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else it deems appropriate. Read
  33.      * {@link Thread#sleep(long, int)}} for further interesting details.
  34.      * </p>
  35.      *
  36.      * @param duration the sleep duration.
  37.      * @throws InterruptedException if interrupted.
  38.      * @see Thread#sleep(long, int)
  39.      */
  40.     public static void sleep(final Duration duration) throws InterruptedException {
  41.         // Using this method avoids depending on the vagaries of the precision and accuracy of system timers and schedulers.
  42.         try {
  43.             // Use the JVM elapsed time, avoids issues with DST changes and manual OS time changes.
  44.             final long nanoStart = System.nanoTime();
  45.             final long finishNanos = nanoStart + duration.toNanos(); // toNanos(): Possible ArithmeticException, otherwise wrap around OK.
  46.             Duration remainingDuration = duration;
  47.             long nowNano;
  48.             do {
  49.                 Thread.sleep(remainingDuration.toMillis(), getNanosOfMilli(remainingDuration));
  50.                 nowNano = System.nanoTime();
  51.                 remainingDuration = Duration.ofNanos(finishNanos - nowNano);
  52.             } while (nowNano - finishNanos < 0); // handles wrap around, see Thread#sleep(long, int).
  53.         } catch (final ArithmeticException e) {
  54.             // Use the current time
  55.             final Instant finishInstant = Instant.now().plus(duration);
  56.             Duration remainingDuration = duration;
  57.             do {
  58.                 Thread.sleep(remainingDuration.toMillis(), getNanosOfMilli(remainingDuration));
  59.                 remainingDuration = Duration.between(Instant.now(), finishInstant);
  60.             } while (!remainingDuration.isNegative());
  61.         }
  62.     }

  63.     /**
  64.      * Make private in 3.0.
  65.      *
  66.      * @deprecated TODO Make private in 3.0.
  67.      */
  68.     @Deprecated
  69.     public ThreadUtils() {
  70.         // empty
  71.     }
  72. }