Skip to content

Instantly share code, notes, and snippets.

@gansai
Created September 2, 2015 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gansai/6a3d969ac185b8e788dc to your computer and use it in GitHub Desktop.
Save gansai/6a3d969ac185b8e788dc to your computer and use it in GitHub Desktop.
Illustration of ListeningExecutorService
class ListeningExecutorServiceIllus
{
//Just for illustration purpose, not a working version.
//Creation of ListeningExecutorService
class MyCallbackRunnable implements Runnable
{
@Override
public void run() {
System.out.println(" I got called by a listenable future");
}
}
public void illustrationMethod()
{
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
//Creation of ListenableFuture
ListenableFuture listeningFuture = service.submit(new Callable() {
public ReturnType call() {
Thread.sleep(20000);
ReturnType returnVariable = new ReturnType();
return returnVariable;
}
});
//Creation of callback executor
Executor mycallbackExecutor = Executors.newFixedThreadPool(10);
//Registering callbacks with listenableFuture
listeningFuture.addListener(new MyCallbackRunnable(),mycallbackExecutor );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment