2013-05-17 12 views
6

से प्रतिक्रिया के रूप में भविष्य को संभालना भविष्य में यह कोड है जो काम करता है। यह एक अभिनेता (ग्रीटर) को एक संदेश भेजता है और जवाब का इंतजार कर रहा है। लेकिन यह वर्तमान धागे को अवरुद्ध करता है।अक्का अभिनेता

public class Future1Blocking { 

    public static void main(String[] args) throws Exception { 

     ActorSystem system = ActorSystem.create("system"); 
     final ActorRef actorRef = system.actorOf(Props.create(Greeter.class), "greeter"); 

     Timeout timeout = new Timeout(Duration.create(5, "seconds")); 
     Future<Object> future = Patterns.ask(actorRef, Greeter.Msg.GREET, timeout); 

     // this blocks current running thread 
     Greeter.Msg result = (Greeter.Msg) Await.result(future, timeout.duration()); 

     System.out.println(result); 

    } 
} 

मेरी उदाहरण future.onSuccess उपयोग करने के लिए वर्तमान कॉल की धागा को रोके बिना परिणाम प्राप्त करने के लिए संभव तरीका क्या है?

उत्तर

10

अहह। यह आसान था (माफ करना)।

future.onSuccess(new PrintResult<Object>(), system.dispatcher()); 

कहाँ:

public final class PrintResult<T> extends OnSuccess<T> { 
    @Override public final void onSuccess(T t) { 
     System.out.println(t); 
    } 
} 
संबंधित मुद्दे