Skip to content

Instantly share code, notes, and snippets.

@vegaasen
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save vegaasen/581331579e7d4f46cce5 to your computer and use it in GitHub Desktop.

Select an option

Save vegaasen/581331579e7d4f46cce5 to your computer and use it in GitHub Desktop.
Asynchronous interface
public interface AsyncHandler {
<T> Future<T> submit(Callable<T> callable);
}
public class AsyncHandlerImpl implements AsyncHandler {
private static final int NUM_THREADS = 5;
/* USING EXECUTORS (java 1.6) */
private ExecutorService pool = Executors.newFixedThreadPool(NUM_THREADS);
/* USING EXECUTORS (java 1.7) */
private final ForkJoinPool pool = new ForkJoinPool(NUM_THREADS);
@Override
public <T> Future<T> submit(Callable<T> callable) {
return pool.submit(callable);
}
}
//usage
private class DoSomethingWrapper implements Callable {
public DoSomethingWrapper() {
}
@Override
public Object call() throws Exception {
return someService.doSomething();
}
}
asyncHandler.submit(new DoSomethingWrapper());
@vegaasen
Copy link
Author

Updated with ForkJoin example as well :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment