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, which inherits from Future, allows us to add callback functions to return values or methods immediately after the thread operation is completed or the method execution is completed.
Add callback functions to ListenableFuture:
Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)
Among them, FutureCallback is an interface that includes onSuccess(V), onFailure(Throwable).
Use as follows:
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.
Ci-dessous se trouve un exemple de test pour 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()); }
Page d'accueil des documents officiels :https://awk.so/@code.google.com!/p/guava-bibliothèques/wiki/ListenableFutureExplained
Voici la présentation de Guava - Rassemblement de documents sur la programmation par lots Futures, continuerons à compléter les documents pertinents, merci de votre soutien à ce site !
Déclaration : le contenu de cet article est issu du réseau, et appartient à ses auteurs respectifs. Le contenu est fourni par les utilisateurs d'Internet de manière spontanée et auto-publiée. Ce site n'appartient pas à lui, n'a pas été édité par l'homme, et n'assume aucune responsabilité juridique. Si vous trouvez du contenu présumé violer les droits d'auteur, n'hésitez pas à envoyer un e-mail à : notice#oldtoolbag.com (veuillez remplacer # par @ lors de l'envoi d'un e-mail pour signaler des violations, et fournissez des preuves pertinentes. Une fois vérifié, ce site supprimera immédiatement le contenu présumé enfreindre les droits d'auteur.