Advertisement
mitrakov

Future: recover vs. andThen

Aug 19th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.38 KB | None | 0 0
  1. // Let's compare Future.recover and Future.andThen
  2. // Try to answer these questions yourself:
  3. // 1. what is the expected type for each future?
  4. // 2. how many "Future1 started" lines expected in stdout?
  5. // 3. both PartialFunctions are not exhausive; what happens if it doesn't match the case?
  6.  
  7. import java.time.LocalDateTime
  8. import scala.concurrent.ExecutionContext.Implicits.global
  9. import scala.concurrent.Future
  10. import scala.io.StdIn
  11. import scala.language.postfixOps
  12. import scala.util.Success
  13.  
  14. object Main extends App {
  15.   def print(x: Any): Unit = {Thread.sleep(50); println(s"${LocalDateTime.now()}: $x")}
  16.  
  17.   def f1 = Future {
  18.     print("Future1 started")
  19.     Thread.sleep(5000)
  20.     print("Future1 finished")
  21.     5 / 2
  22.   }
  23.  
  24.   val recoveredFuture: Future[AnyVal] = f1.recover {   // AnyVal is common type for (Int, Long)
  25.     case ex: ArithmeticException => println(s"Divide by 0 exception: $ex"); Long.MaxValue
  26.   }
  27.  
  28.   val sideEffectFuture: Future[Int] = f1.andThen {
  29.     case Success(x) => println(s"Result is $x")
  30.   }
  31.  
  32.   print("Press ENTER to finish")
  33.   StdIn.readLine()
  34.   print(s"Results: $recoveredFuture; $sideEffectFuture")
  35. }
  36.  
  37.         // 1. Interrupted futures:
  38.         // 2018-08-19T18:12:55.976: Future1 started
  39.         // 2018-08-19T18:12:55.983: Future1 started
  40.         // 2018-08-19T18:12:55.984: Press ENTER to finish
  41.         //
  42.         // 2018-08-19T18:12:57.250: Results: Future(<not completed>); Future(<not completed>)
  43.  
  44.         // 2. Successful execution:
  45.         // 2018-08-19T18:13:07.345: Press ENTER to finish
  46.         // 2018-08-19T18:13:07.345: Future1 started
  47.         // 2018-08-19T18:13:07.345: Future1 started
  48.         // 2018-08-19T18:13:12.400: Future1 finished
  49.         // 2018-08-19T18:13:12.404: Future1 finished
  50.         // Result is 2
  51.        
  52.         // 2018-08-19T18:13:13.328: Results: Future(Success(2)); Future(Success(2))
  53.        
  54.         // 3. Failed execution:
  55. 5 / 0
  56.         // 2018-08-19T18:13:29.086: Future1 started
  57.         // 2018-08-19T18:13:29.086: Future1 started
  58.         // 2018-08-19T18:13:29.086: Press ENTER to finish
  59.         // 2018-08-19T18:13:34.140: Future1 finished
  60.         // 2018-08-19T18:13:34.140: Future1 finished
  61.         // Divide by 0 exception: java.lang.ArithmeticException: / by zero
  62.         //
  63.         // 2018-08-19T18:13:36.240: Results: Future(Success(9223372036854775807)); Future(Failure(java.lang.ArithmeticException: / by zero))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement