Java Duration Calculate runAfter(Duration duration, Runnable toRun)

Here you can find the source of runAfter(Duration duration, Runnable toRun)

Description

Schedules a runnable after a given time

License

Apache License

Parameter

Parameter Description
duration The duration to timeout after.

Return

A completable future that will time out.

Declaration

public static void runAfter(Duration duration, Runnable toRun) 

Method Source Code

    //package com.java2s;
    //License from project: Apache License 

    import com.google.common.util.concurrent.ThreadFactoryBuilder;
    import java.time.Duration;
    import java.util.concurrent.*;

    public class Main {
        private static final ScheduledExecutorService scheduler = Executors
                .newScheduledThreadPool(1, new ThreadFactoryBuilder()
                        .setDaemon(true).setNameFormat("failAfter-%d").build());

        /** Schedules a runnable after a given time
         * @param duration  The duration to timeout after.
         * @return          A completable future that will time out.
         */
        public static void runAfter(Duration duration, Runnable toRun) {
    final CompletableFuture<Void> promise = new CompletableFuture<>();
    scheduler.schedule(toRun::run, duration.toMillis(), TimeUnit.MILLISECONDS);
}
    }/*from  ww w.ja v a 2  s . c  om*/

Related

  1. isInfPositive(Duration duration)
  2. max(Duration a, Duration b)
  3. min(Duration a, Duration b)
  4. minutesToDuration(long minutes)
  5. multiply(Duration duration, double factor)
  6. seconds(Duration duration)
  7. serializeDuration(Duration duration)
  8. sleep(Duration duration)
  9. sleep(final Duration duration)