Akka working with the future Functionally_map method
Look at the code,
Package com.usoft;import akka.dispatch.executioncontexts;import akka.dispatch.futures;import akka.dispatch.mapper;import akka.dispatch.oncomplete;import akka.dispatch.onfailure;import akka.dispatch.OnSuccess;import scala.concurrent.ExecutionContextExecutorService;import scala.concurrent.future;import java.util.concurrent.callable;import java.util.concurrent.executors;/ ** * created by liyanxin on 2015/1/8. */public class futuredemo { public static void main (string args[]) { The // execution context can specify the thread pool type itself // needs a executioncontext as its executor // future Need a executioncontext, it's very similar to java.util.concurrent.executor . // If you have a ac within the scopetorsystem , It can be used as a executioncontext with System.dispatcher (). // You can also use executioncontext // the factory method provided by the associated object to bring Executors and ExecutorServices to the parcel, Or even create your own instances. executioncontextexecutorservice ec = ExecutionContexts. fromexecutorservice (Executors.newcachedthreadpool ()); future<string> f1 = futures.future (new callable<string> () { public string call () throws InterruptedException { thread.sleep ( //), which blocks within the current thread for 5 seconds &NBSP;&NBSP;&NBSP;&NBsp; system.out.println ( Thread.CurrentThread (). GetName () + " thread end| | | F1 "); return "Hello" + "World"; } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;},&NBSP;EC); /* * * through the Map method f1 -> f2 * Now we have a second Future, * f2, that will eventually contain an Integer. When our original Future, f1, completes, it will also apply our * function and complete the second future with its result. When we finally get the result, it will contain the * number 10. our original future still contains the string "HelloWorld" and is unaffected by the map. */ future<integer> f2 = f1.map (new mapper<string, integer> () { public Integer Apply (String s) { system.out.println (Thread.CurrentThread (). GetName () + " thread end| | | F1->f2 "); &nbSp; return s.length (); } }, &NBSP;EC); f2.oncomplete (New oncomplete<integer> () { @Override public void oncomplete (Throwable failure, integer success) throws Throwable { system.out.print (Thread.CurrentThread (). GetName () + " thread end| | |"); System.out.println ("f2 on complete=" + success); &nBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;},&NBSP;EC); f2.onsuccess (new onsuccess<integer> () { @Override public void onsuccess (Integer result) throws Throwable { system.out.print ( Thread.CurrentThread (). GetName () + " thread end| | |"); System.out.println ("The length of the returned result =" + result); &NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;},&NBSP;EC); f2.onfailure (New onfailure () { @Override public void onfailure (throwable failure) throws Throwable { System.out.print (Thread.CurrentThread (). GetName () + " thread end| | |"); System.out.println ("f2 failure=" + failure.getmessage ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;},&NBSP;EC); system.out.println (Thread.CurrentThread (). GetName () + Thread end "); }}
Run the results,
Main thread End
Pool-1-thread-1 Thread end| | | F1
Pool-1-thread-2 Thread end| | | F1->f2
Pool-1-thread-1 Thread end| | | F2 on complete=10
pool-1-thread-3 Thread end| | | Returns the length of the result =10
============================end============================
Akka working with the future Functionally_map method