Permalink
Browse files

First steps with futures in Scala

  • Loading branch information...
1 parent 5041091 commit b8676d86d65a2954e950fa9307b7b87c3d794af5 @Srirangan committed Jan 9, 2013
Showing with 114 additions and 0 deletions.
  1. +6 −0 app/views/index.scala.html
  2. +108 −0 public/posts/2013-01-first-steps-with-scala-futures.html
@@ -3,6 +3,12 @@
<div class='span9'>
<ul class="unstyled">
<li>
+ <a href="/2013-01-first-steps-with-scala-futures">
+ First steps with `futures` in Scala
+ </a>
+ <small>2012-12-05</small>
+ </li>
+ <li>
<strong><em>Happy New Year! =D</em></strong>
<small>2013-01-01</small>
</li>
@@ -0,0 +1,108 @@
+<h2>First steps with `futures` in Scala</h2>
+
+<div class="entry-content">
+ <p>The recently released Scala 2.10.0 introduces <code>futures</code> and <code>promises</code>.</p>
+ <p>One of the most prominent highlights of this release, futures make it extremely trivial to compose asynchronous multi-threaded apps.</p>
+ <p>And you can do so elegantly as I'll try to demonstrate with a few example.</p>
+ <pre>
+ // example 1
+ //
+ // `future` blocks asynchronously execute in a different thread and return a `val`
+ // asynchronous results can be trapped via `onSuccess`, `onFailure` and `onComplete` callbacks
+
+ Console.println("Waiting..")
+
+ val sayHello = future {
+ Thread.sleep(1000)
+ "hello"
+ }
+
+ sayHello onSuccess {
+ case message => Console.println(s"He said '$message'")
+ }
+
+ Thread.sleep(2000)
+
+ Console.println("Try dividing by zero")
+
+ val tryDivideByZero = future {
+ Thread.sleep(1000)
+ 1 / 0
+ }
+
+ tryDivideByZero onFailure {
+ case e: ArithmeticException => Console.println(s"Don't be silly!")
+ }
+
+ Thread.sleep(2000)
+ </pre>
+ <pre>
+ // example 2
+ //
+ // if one future depends on the result of another
+ // you could nest callbacks
+ // or use `map` to "chain" them
+ // `map` returns a future which can be trapped via callbacks
+
+ val firstLove = future {
+ Thread.sleep(500)
+ "i love you"
+ }
+
+ val thenBetray = firstLove map {
+ case loveLetter => {
+ Console.println(loveLetter)
+ Thread.sleep(500)
+ "not really"
+ }
+ }
+
+ thenBetray onSuccess {
+ case partingWords => Console.println(partingWords)
+ }
+
+ Thread.sleep(2000)
+ </pre>
+ <pre>
+ // example 3
+ //
+ // multiple inter-dependent futures typically create a nested callback mess
+ // this is avoided with the `for` construct
+ // `for` can "chain" multiple futures, and "apply" another `future` on their results
+
+ val calculateMyScore = future {
+ Thread.sleep(500)
+ new Random().nextInt(10)
+ }
+
+ val calculateYourScore = future {
+ Thread.sleep(500)
+ new Random().nextInt(10)
+ }
+
+ val doIWin = for {
+ myScore <- calculateMyScore
+ yourScore <- calculateYourScore
+ } yield myScore > yourScore
+
+
+ doIWin onSuccess {
+ case b: Boolean => Console.println(if (b) "yes" else "no")
+ }
+
+ Console.println("Do I win?")
+
+ Thread.sleep(2000)
+ </pre>
+
+ <p>
+ The entire example - <a href="https://github.com/Srirangan/scala-playground/blob/master/src/main/scala/net/srirangan/scalaplayground/futures/Main.scala">available
+ on GitHub</a> - is an executable application.
+ </p>
+
+ <p>The main app runs on its own thread and each future block uses a thread from the global execution context thread pool.</p>
+
+ <p>Timers <code>Thread.sleep(...)</code> are included to simulate asynchronous calls.</p>
+
+ <p>For further reading I recommend the <a href="http://docs.scala-lang.org/overviews/core/futures.html">Futures and Promises</a> essay on Scala Docs.</p>
+</div>

0 comments on commit b8676d8

Please sign in to comment.