English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Guava provides many useful extensions for Java parallel programming Future, mainly through the ListenableFuture interface, and with the help of Futures static extensions.
ListenableFuture, inheriting from Future, allows us to add callback functions that return values or methods immediately when the thread operation is completed or when the method execution is completed.
Add callback functions to ListenableFuture:
Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)
where FutureCallback is an interface that includes onSuccess(V), onFailure(Throwable).
Use, for example:
Futures.addCallback(ListenableFuture, new FutureCallback<Object>() { public void onSuccess(Object result) { System.out.printf("onSuccess with: %s%n", result); } public void onFailure(Throwable thrown) { System.out.printf("onFailure %s%n", thrown.getMessage()); } });
At the same time, Guava provides many useful extensions for Future extensions, mainly through the ListenableFuture interface, and with the help of Futures static extensions.
The following is a test demo for Future:
@Test public void should_test_furture() throws Exception { ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); ListenableFuture future1 = service.submit(new Callable<Integer>() { public Integer call() throws InterruptedException { Thread.sleep(1000); System.out.println("call future 1."); return 1; } }); ListenableFuture future2 = service.submit(new Callable<Integer>() { public Integer call() throws InterruptedException { Thread.sleep(1000); System.out.println("call future 2."); // throw new RuntimeException("----call future 2."); return 2; } }); final ListenableFuture allFutures = Futures.allAsList(future1, future2); final ListenableFuture transform = Futures.transform(allFutures, new AsyncFunction<List<Integer>, Boolean>() { @Override public ListenableFuture apply(List<Integer> results) throws Exception { return Futures.immediateFuture(String.format("success future:%d", results.size())); } }); Futures.addCallback(transform, new FutureCallback<Object>() { public void onSuccess(Object result) { System.out.println(result.getClass()); System.out.printf("success with: %s%n", result); } public void onFailure(Throwable thrown) { System.out.printf("onFailure%s%n", thrown.getMessage()); } }); System.out.println(transform.get()); }
Official material homepage:https://awk.so/@code.google.com!/p/guava-libraries/wiki/ListenableFutureExplained
The above is the material on Guava - Material compilation of parallel programming Futures, more related materials will be supplemented later. Thank you all for your support to this site!
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)