Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #185 Execute function in 30 seconds

Schedule the execution of f(42) in 30 seconds.

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.schedule(() -> _f(42), delayInSeconds, TimeUnit.SECONDS);
import static java.lang.Thread.sleep;
Thread t = new Thread(() -> {
    try {
        sleep(30_000);
        f(42);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
});
t.start();
(do (Thread/sleep 30000)
    (f 42))

Clojure is a hosted language, and relies on host interop for things like this

New implementation...
< >
programming-idioms.org