Here you can find the source of runAfter(Duration duration, Runnable toRun)
Parameter | Description |
---|---|
duration | The duration to timeout after. |
public static void runAfter(Duration duration, Runnable toRun)
//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*/