Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Suppose we've got a Future[T] and usual blocking code:
- import java.time.LocalDateTime
- import scala.concurrent.ExecutionContext.Implicits.global
- import scala.concurrent.{Await, Future}
- import scala.concurrent.duration._
- import scala.io.StdIn
- import scala.language.postfixOps
- import scala.util.control.NonFatal
- object Main extends App {
- def f(x: Int, y: Int): Future[Int] = Future {
- println(LocalDateTime.now() + ": Start calculation!")
- Thread.sleep(3000)
- println(LocalDateTime.now() + ": Done calculation!")
- x / y
- }
- try {
- println(LocalDateTime.now() + ": Hello!")
- val resultFut = f(5, 2)
- val result = Await.result(resultFut, 5 seconds)
- println(LocalDateTime.now() + s": Result is $result")
- } catch {
- case e: ArithmeticException => System.err.println(s"Arithmetic err! $e")
- case NonFatal(e) => println(s"Error: $e")
- }
- println(LocalDateTime.now() + ": Press ENTER to finish!")
- StdIn.readLine()
- println(LocalDateTime.now() + ": Bye!")
- }
- // Output:
- // 2018-01-17T22:21:46.561: Hello!
- // 2018-01-17T22:21:46.690: Start calculation!
- // 2018-01-17T22:21:49.690: Done calculation!
- // 2018-01-17T22:21:49.692: Result is 2
- // 2018-01-17T22:21:49.695: Press ENTER to finish!
- // 2018-01-17T22:21:52.840: Bye!
- // 2. Now try to fail and ensure it works properly:
- val resultFut = f(5, 0)
- // Output:
- // 2018-01-17T22:23:18.419: Hello!
- // 2018-01-17T22:23:18.543: Start calculation!
- // 2018-01-17T22:23:21.543: Done calculation!
- // Arithmetic err! java.lang.ArithmeticException: / by zero
- // 2018-01-17T22:23:21.549: Press ENTER to finish!
- // 2018-01-17T22:23:24.544: Bye!
- // 3. Let's make it async!
- println(LocalDateTime.now() + ": Hello!")
- f(5, 2) onComplete {
- case Success(result) => println(LocalDateTime.now() + s": Result is $result")
- case Failure(e: ArithmeticException) => System.err.println(s"Arithmetic err! $e")
- case Failure(NonFatal(e)) => println(s"Error: $e")
- }
- // Output (note the order of lines!):
- // 2018-01-17T22:26:25.523: Hello!
- // 2018-01-17T22:26:25.645: Press ENTER to finish!
- // 2018-01-17T22:26:25.645: Start calculation!
- // 2018-01-17T22:26:28.645: Done calculation!
- // 2018-01-17T22:26:28.645: Result is 2
- // 2018-01-17T22:26:30.726: Bye!
- // 4. Let's check failures:
- f(5, 0)
- // Output (note the order of lines!):
- // 2018-01-17T22:26:54.466: Hello!
- // 2018-01-17T22:26:54.603: Press ENTER to finish!
- // 2018-01-17T22:26:54.604: Start calculation!
- // 2018-01-17T22:26:57.604: Done calculation!
- // Arithmetic err! java.lang.ArithmeticException: / by zero
- // 2018-01-17T22:26:59.366: Bye!
- // 5. Now what if we need to return a result? way #1: with 'transform' function:
- object Main extends App {
- def f(x: Int, y: Int): Future[Int] = Future {
- println(LocalDateTime.now() + ": Start calculation!")
- Thread.sleep(3000)
- println(LocalDateTime.now() + ": Done calculation!")
- x / y
- }
- println(LocalDateTime.now() + ": Hello!")
- val strResult = f(5, 0) transform {
- case Success(result) => Success(LocalDateTime.now() + s": Result is $result")
- case Failure(e: ArithmeticException) => Success(s"Arithmetic err! $e")
- case Failure(NonFatal(e)) => Success(s"Error: $e")
- }
- strResult foreach println // prints a result as soon as Future finishes
- println(LocalDateTime.now() + ": Press ENTER to finish!")
- StdIn.readLine()
- println(LocalDateTime.now() + ": Bye!")
- }
- // Output:
- // 2018-01-17T22:33:45.196: Hello!
- // 2018-01-17T22:33:45.333: Start calculation!
- // 2018-01-17T22:33:45.334: Press ENTER to finish!
- // 2018-01-17T22:33:48.333: Done calculation!
- // Arithmetic err! java.lang.ArithmeticException: / by zero
- // 2018-01-17T22:33:50.794: Bye!
- // 6. And way #2: with 'map-recover' functions:
- object Main extends App {
- def f(x: Int, y: Int): Future[Int] = Future {
- println(LocalDateTime.now() + ": Start calculation!")
- Thread.sleep(3000)
- println(LocalDateTime.now() + ": Done calculation!")
- x / y
- }
- println(LocalDateTime.now() + ": Hello!")
- val strResult = f(5, 0) map { result =>
- LocalDateTime.now() + s": Result is $result"
- } recover {
- case e: ArithmeticException => s"Arithmetic err! $e"
- case NonFatal(e) => s"Error: $e"
- }
- strResult foreach println // prints a result as soon as Future finishes
- println(LocalDateTime.now() + ": Press ENTER to finish!")
- StdIn.readLine()
- println(LocalDateTime.now() + ": Bye!")
- }
- // Output:
- // 2018-01-17T22:34:42.673: Hello!
- // 2018-01-17T22:34:42.805: Start calculation!
- // 2018-01-17T22:34:42.807: Press ENTER to finish!
- // 2018-01-17T22:34:45.806: Done calculation!
- // Arithmetic err! java.lang.ArithmeticException: / by zero
- // 2018-01-17T22:34:47.059: Bye!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement