Skip to content

Instantly share code, notes, and snippets.

@stacktracer
Created January 27, 2010 17:40
Show Gist options
  • Save stacktracer/288035 to your computer and use it in GitHub Desktop.
Save stacktracer/288035 to your computer and use it in GitHub Desktop.
public static <V> V callInterruptibly(Callable<V> callable) throws InterruptedException
{
ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory()
{
public Thread newThread(Runnable runnable)
{
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
thread.setDaemon(true);
return thread;
}
});
Future<V> future = executor.submit(callable);
try
{
return future.get();
}
catch (ExecutionException e)
{
throw new RuntimeException(e);
}
finally
{
executor.shutdownNow();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment