Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Scala 2.9.3 and later</h2> <p>THe simplest approach is to use the <code>scala.concurrent.Future</code> class and associated infrastructure. The <code>scala.concurrent.future</code> method asynchronously evaluates the block passed to it and immediately returns a <code>Future[A]</code> representing the asynchronous computation. Futures can be manipulated in a number of non-blocking ways, including mapping, flatMapping, filtering, recovering errors, etc.</p> <p>For example, here's a sample that creates 10 tasks, where each tasks sleeps an arbitrary amount of time and then returns the square of the value passed to it.</p> <pre><code>import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global val tasks: Seq[Future[Int]] = for (i &lt;- 1 to 10) yield future { println("Executing task " + i) Thread.sleep(i * 1000L) i * i } val aggregated: Future[Seq[Int]] = Future.sequence(tasks) val squares: Seq[Int] = Await.result(aggregated, 15.seconds) println("Squares: " + squares) </code></pre> <p>In this example, we first create a sequence of individual asynchronous tasks that, when complete, provide an int. We then use <code>Future.sequence</code> to combine those async tasks in to a single async task -- swapping the position of the <code>Future</code> and the <code>Seq</code> in the type. Finally, we block the current thread for up to 15 seconds while waiting for the result. In the example, we use the global execution context, which is backed by a fork/join thread pool. For non-trivial examples, you probably would want to use an application specific <code>ExecutionContext</code>.</p> <p>Generally, blocking should be avoided when at all possible. There are other combinators available on the <code>Future</code> class that can help program in an asynchronous style, including <code>onSuccess</code>, <code>onFailure</code>, and <code>onComplete</code>.</p> <p>Also, consider investigating the <a href="http://akka.io" rel="noreferrer">Akka</a> library, which provides actor-based concurrency for Scala and Java, and interoperates with <code>scala.concurrent</code>.</p> <h2>Scala 2.9.2 and prior</h2> <p>This simplest approach is to use Scala's Future class, which is a sub-component of the Actors framework. The scala.actors.Futures.future method creates a Future for the block passed to it. You can then use scala.actors.Futures.awaitAll to wait for all tasks to complete.</p> <p>For example, here's a sample that creates 10 tasks, where each tasks sleeps an arbitrary amount of time and then returns the square of the value passed to it.</p> <pre><code>import scala.actors.Futures._ val tasks = for (i &lt;- 1 to 10) yield future { println("Executing task " + i) Thread.sleep(i * 1000L) i * i } val squares = awaitAll(20000L, tasks: _*) println("Squares: " + squares) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload