Java tutorial
//package com.java2s; import com.google.common.base.Throwables; import java.util.concurrent.*; public class Main { private static final ExecutorService POOL = Executors.newCachedThreadPool(); public static Future<?> timeout(long millis, Runnable runnable) { return POOL.submit(() -> { sleep(millis); runnable.run(); }); } public static <T> Future<T> timeout(long millis, Callable<T> callable) { return POOL.submit(() -> { sleep(millis); return callable.call(); }); } public static void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { throw Throwables.propagate(e); } } }