Skip to content

Instantly share code, notes, and snippets.

@wy8162
Created July 17, 2015 20:17
Show Gist options
  • Select an option

  • Save wy8162/d41c6157ae6a0e1f2a4e to your computer and use it in GitHub Desktop.

Select an option

Save wy8162/d41c6157ae6a0e1f2a4e to your computer and use it in GitHub Desktop.
RxJava
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class FuturesA {
/**
* https://gist.github.com/benjchristensen/4670979
*/
public static void run() throws Exception {
ExecutorService executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
Future<String> f1 = executor.submit(new CallToRemoteServiceA());
Future<String> f2 = executor.submit(new CallToRemoteServiceB());
System.out.println(f1.get() + " - " + f2.get());
}
private static final class CallToRemoteServiceA implements Callable<String> {
@Override
public String call() throws Exception {
// simulate fetching data from remote service
Thread.sleep(100);
return "responseA";
}
}
private static final class CallToRemoteServiceB implements Callable<String> {
@Override
public String call() throws Exception {
// simulate fetching data from remote service
Thread.sleep(40);
return "responseB";
}
}
}
def r = new FuturesA();
r.run();
@wy8162

wy8162 commented Jul 17, 2015

Copy link
Copy Markdown
Author

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