Possible Duplicate:
What is the Scala equivalent of F#'s async workflows?
Is there an equiv开发者_如何转开发alent to F# async workflows in Scala ? I would like to use this to crawl webpages concurrently.
Thank you
To execute a task asynchronously in parallel, simply use Actor.actor
method:
import scala.actors.Actor._
actor {
// code here is excuted asynchronously
}
To execute a task and wait for a result, use futures:
import scala.actors.Futures.future
val f = future {
// code here is excuted asynchronously
// last expression is returned
}
//... other code
val result = f() // block until f is completed and return the value
For more complex workflows, have a look at Scala (or Akka) actors. You can also have a look at java NIO which allows async IO operations.
精彩评论