Guava는 Java Parallel Programming Future를위한 유용한 확장을 제공하며, 주요 인터페이스는 ListenableFuture이며, 선물의 도움으로 정적으로 확장됩니다.
ListenableFuture는 미래에 상속되면 콜백 함수를 추가하여 스레드 작업이 완료되거나 메소드 실행이 즉시 반환 될 때 값을 반환 할 수 있습니다.
ListenableFuture에 콜백 함수 추가 :
futures.addcallback (ListenableFuture <v>, futurecallback <v>, 집행자)
FutureCallback은 OnSuccess (V) 및 OnFailure (Throwable)를 포함하는 인터페이스입니다.
다음과 같이 사용 :
futures.addCallback (ListenableFuture, New FutureCallback <botorback <botorbect () {public void onsuccess (Object) {System.out.printf ( "onsuccess with : %s %n", 결과);} public void onfailure (system.out.printf (thrown); }});동시에 구아바의 미래는 다음과 같습니다.
아래는 향후 테스트 데모입니다.
@testpublic void duber_test_furture ()는 예외를 {listenecutorservice service = morexecutors.listeningedecorator (executors.newfixedthreadpool (10)); ListenableFuture Future1 = service.submit (new Callable <integer> () {public integer call () 던지기 중국어 {strule.sleep (1000); System.out.println ( "Call 1."); return 1;}}); ListenableFuture Future2 = service.submit (new Callable <integer> () {public integer call () 던지기 {thread.sleep (1000); System.out.println ( "Call Future 2."); // 새로운 runtimeexception ( "---- 전화 미래 2."); return 2;}}); 최종 청취 가능 문제 allfuture = futures.allaslist (Future1, Future2); 최종 청취 가능 문제 변환 = futures.Transform (Allfutures, New Asyncfunction <list <integer>, boolean> () {@override public listablefuture apply (list <integer> results) 예외 {return futures.immediateFuture ( "성공 미래 :%d", results.size ()); futures.addcallback (변환, 새로운 futurecallback <botort> () {public void onsuccess (object) {system.out.println (result.getclass ()); System.out.printf ( "성공 :%s%n", 결과);} public void onfailure (System.out.out.out.out.print.print. thrown.getMessage ()); System.out.println (transform.get ());}공식 정보 홈페이지 : https://awk.so/@code.google.com !/p/guava-libraries/wiki/listenablefutureExplained
위의 것은 Guava -Parallel 프로그래밍 선물을 분류하는 정보입니다. 우리는 향후 관련 정보를 계속 추가 할 것입니다. 이 웹 사이트를 지원 해주셔서 감사합니다!