Question : java settimeout
Answered by : serge-kalaga
Try :
public static void setTimeout(Runnable runnable, int delay){ new Thread(() -> { try { Thread.sleep(delay); runnable.run(); } catch (Exception e){ System.err.println(e); } }).start();
}
To called with lambda exception:
setTimeout(() -> System.out.println("test"), 1000);
Source : | Last Update : Fri, 08 Apr 22
Question : java timeout
Answered by : cheerful-crayfish-0y4qyf502rur
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); final Future handler = executor.submit(new Callable(){ ... }); executor.schedule(new Runnable(){ public void run(){ handler.cancel(); } }, 10000, TimeUnit.MILLISECONDS);
Source : | Last Update : Thu, 10 Feb 22