Logo

Programming-Idioms

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

Idiom #185 Execute function in 30 seconds

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

use threads;
my $thr = threads->create(sub { sleep(30); f(42); });
$thr->join()
local $SIG{ALRM} = sub { f(42) };
alarm 30;
use threads;
my $thr = threads->create(sub { sleep(30); f(42); });
$thr->detach();
(do (Thread/sleep 30000)
    (f 42))
using System;
using System.Threading.Tasks;
Task.Delay(TimeSpan.FromSeconds(30))
    .ContinueWith(_ => f(42));
Future.delayed(const Duration(seconds: 30), () => f(42));
import "time"
timer := time.AfterFunc(
	30*time.Second,
	func() {
		f(42)
	})
import "time"
go func() {
	time.Sleep(30 * time.Second)
	f(42)
}()
sleep(30*1000)
f(42)
let id = setTimeout(f, 30000, 42);
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();
sleep(30);
f(42);
import threading
timer = threading.Timer(30.0, f, args=(42,) ) 
timer.start() 
Thread.new do
  sleep 30
  f(42)
end
use std::time::Duration;
use std::thread::sleep;
sleep(Duration::new(30, 0));
f(42);
(f future: 30 "seconds" * 1000) value: 42.

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