Java tutorial
//package com.java2s; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class Main { private static <T> T callWithTimeout(final Callable<T> c, long timeout, ExecutorService timeoutExecutor) throws InterruptedException, ExecutionException, TimeoutException { FutureTask<T> task = new FutureTask<T>(c); timeoutExecutor.execute(task); return task.get(timeout, TimeUnit.MILLISECONDS); } }