使用scala.concurrent.Await
(JavaDoc)。也许您正在寻找以下草图?
import scala.concurrent.Future
import scala.concurrent.ExecutionContext._
import scala.concurrent.duration.Duration
import scala.concurrent.duration._
import scala.concurrent.Await
import java.util.concurrent.Executors.newScheduledThreadPool
object Test {
implicit val executor = fromExecutorService(newScheduledThreadPool(10))
val timeout = 30 seconds
def main(args: Array[String]) {
val f =
// Make HTTP request, which yields a string (say); simulated here as follows
Future[String] {
Thread.sleep(1000)
"Response"
}.
// Read input
map {
response =>
println("Please state your problem: ")
val ln = readLine() // Note: this is blocking.
(response, ln)
}.
// Futher processing
map {
case (response, input) =>
println("Response: " + response + ", input: " + input)
}
Await.ready(f, timeout)
}
}