ExecutorService service = Executors.newSingleThreadExecutor();
Runnable task = () -> {
System.out.println("start");
try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
};
Future<?> future = service.submit(task);
try {
future.get(4, TimeUnit.SECONDS);
System.out.println("end...");
} catch (Exception e) {
e.printStackTrace();
}