Scala Akka future sequence execution sequential execution

Source: Internet
Author: User

For a, B, and C, the Akka is automatically executed sequentially by default, but for database operations we want to execute a few operations in order, and we need to use syntax to declare

There are two ways to declare a future relationship, the first is FlatMap, and the second is for

ImportScala.Concurrent.FutureImportScala.Concurrent.ExecutionContext.Implicits.GlobalImportScala.Concurrent.BlockingImportScala.Concurrent.Duration._DefF(Item: Int): Future[Unit] = Future{Print("Waiting" +Item+ "Seconds ...") Console.Flush blocking{Thread.Sleep((Item seconds).Tomillis)}println("Done")}ValFserial=F(4)FlatMap(Res1=F(16 Flatmap (res2 =>< Span class= "PLN" > F (2 FlatMap  (res3 => F (8fserial.{case Restry =>  Println ( "!!!! That ' s a wrap!!!! success= " + Restry. 

ValFserial= For {Res1<-F(4);Res2<-F(16 Res3 <- F (2 res4 <- F (8)} yield  "did it!" fserial.{case Restry =>  Println ( "!!!! That ' s a wrap!!!! success= " + Restry.             
            

For can be understood as FLATMAP syntax sugar, from the written code view, for clearer, intuitive

It is important to note that iterator and future cannot be placed in a for loop, then a for loop, either iterator or a future

Scala Akka future sequence execution sequential execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.