Last active
August 29, 2015 14:03
-
-
Save vegaasen/581331579e7d4f46cce5 to your computer and use it in GitHub Desktop.
Asynchronous interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated with ForkJoin example as well :-).