Guava provides a lot of useful extensions for Java parallel Programming future, its main interface is listenablefuture, and with the help of futures static extension.
Inherit to future Listenablefuture, allow us to add callback function return value or method execution completes immediately when the thread operation completes.
To add a callback function to Listenablefuture:
Futures.addcallback (LISTENABLEFUTURE<V>, Futurecallback<v>, Executor)
Where Futurecallback is an interface that contains onsuccess (V), OnFailure (Throwable).
Use such as:
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 in guava futures for future extensions there are:
- Transform: Converts the return value of Listenablefuture.
- Allaslist: A merge of multiple listenablefuture that returns a list object that returns multiple future return values when all future are successful. Note: When one of the future fails or is canceled, it will go into failure or cancel.
- Successfulaslist: Similar to Allaslist, the only difference is null substitution for failed or canceled future return values. Will not go into failure or cancel the process.
- Immediatefuture/immediatecancelledfuture: Returns a listenablefuture to return the value immediately.
- makechecked: Convert Listenablefuture to Checkedfuture. Checkedfuture is a listenablefuture that contains multiple versions of the Get method, and the method declaration throws a check exception. This makes it easier to create a future that can throw an exception in the execution logic
- Jdkfutureadapters.listeninpoolthread (future): Guava also provides an interface function to convert JDK future to listenablefuture.
Below is a test demo for future:
@Test public void Should_test_furture () throws Exception {Listeningexecutorservice service = Moreexecutors.listeningde
Corator (Executors.newfixedthreadpool (10)); Listenablefuture future1 = service.submit (new callable<integer> () {public Integer call () throws Interruptedexce
ption {thread.sleep (1000);
SYSTEM.OUT.PRINTLN ("Call future 1.");
return 1;
}
}); Listenablefuture Future2 = service.submit (new callable<integer> () {public Integer call () throws Interruptedexce
ption {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) {Syst
Em.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 information home: https://awk.so/@code. google.com!/p/guava-libraries/wiki/listenablefutureexplained
The above is the guava-parallel programming futures data collation, follow-up continue to supplement the relevant information thank you for your support of this site!