public static CompletableFuture < Void > delayedFuture ( Duration delay , ScheduledExecutorService executorService ) { CompletableFuture < Void > result = new CompletableFuture <> ( ) ; if ( delay . toMillis ( ) == 0 ) { // Zero delay; no need to bother with scheduling a task in the future. result . complete ( null ) ; } else { ScheduledFuture < Boolean > sf = executorService . schedule ( ( ) -> result . complete ( null ) , delay . toMillis ( ) , TimeUnit . MILLISECONDS ) ; result . whenComplete ( ( r , ex ) -> sf . cancel ( true ) ) ; } return result ; }
Creates a CompletableFuture that will do nothing and complete after a specified delay without using a thread during the delay .