开发者

Asynchronous coding in Scala [duplicate]

开发者 https://www.devze.com 2023-03-18 07:41 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: What is the Scala equivalent of F#'s async workflows?
This question already has an answer here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号