程序终止前期货不运行 [英] Futures do not run before program termination

查看:62
本文介绍了程序终止前期货不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在新的Scala 2.10上重现该示例未来功能
我使用的代码是:

I was trying to reproduce the example on new Scala 2.10 futures feature. The code I've used is:

import scala.concurrent.Future
import scala.concurrent.future

object Test {
    def main(args: Array[String]) {
     println("Test print before future")
     val s = "Hello"
     val f = future {s + " future!"}
     f onSuccess {case v => println(v)}
     println("Test print after future")
    }
}

代替打印:

Test print before future
Hello future!
Test print after future

它只是打印:

Test print before future
Test print after future


$之后的测试打印b $ b

我为什么会有这种行为的任何想法?我的Scala编译器版本为2.10.0-20120507。

Any idea of why I have this behaviour? My version of scala compiler is 2.10.0-20120507.

推荐答案

问题是您将其作为独立程序执行,其主线程在工作线程之一可以执行 Hello future!之前终止。 println 。 (新的期货库产生的线程是守护程序线程。)

The issue is that you're executing that as a standalone program, whose main thread is terminating before one of the worker threads can execute the "Hello future!" println. (The threads that the new futures library spawns are daemon threads).

您还可以使用 Await 对象(也可以在 scala.concurrent 中)等到将来的 f 完成:

You can also use the Await object (also in scala.concurrent) to wait until the future f is completed:

import scala.concurrent._
import scala.concurrent.util._

object Test {
  def main(args: Array[String]) {
    println("Test print before future")

    val s = "Hello"
    val f = future {s + " future!"}
    f onSuccess {case v => println(v)}
    println("Test print after future")

    Await.ready(f, Duration.Inf)
  }
}

这可以打印:

Test print before future
Test print after future
Hello future!

或者,它可以打印你好,未来!

Or, it can print "Hello future!" before "Test print after future" depending on the thread schedule.

同样,您可以强制主线程等待直到 f 在最后一个 println 之前完成,如下所示:

Likewise, you can force the main thread to wait until f is completed before the last println as follows:

import scala.concurrent._
import scala.concurrent.util._

object Test {
  def main(args: Array[String]) {
    println("Test print before future")

    val s = "Hello"
    val f = future {s + " future!"}
    f onSuccess {case v => println(v)}

    Await.ready(f, Duration.Inf)        

    println("Test print after future")
  }
}

打印哪个:

Test print before future
Hello future!
Test print after future

之后的测试打印

但是,请注意,使用等待,您正在屏蔽。当然,这可以确保您的主应用程序线程不会终止,但除非另有必要,否则通常不应该使用它。

However, note that when you use Await, you're blocking. This of course makes sense to make sure that your main application thread doesn't terminate, but generally shouldn't be used unless necessary otherwise.

Await 对象在这种情况下是必要的逃生舱口,但在整个应用程序代码中使用它时无需担心因为它的语义可能会导致执行速度较慢,并行度较低。例如,如果需要确保以指定的顺序执行回调,则还有其他选择,例如 andThen 未来上使用c>和映射方法。)

(The Await object is a necessary escape hatch for situations like these, but using it throughout application code without concern for its semantics can result in slower, less-parallel execution. If you need to ensure that callbacks are executed in some specified order, for example, there are other alternatives, such as the andThen and map methods on Future.)

这篇关于程序终止前期货不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆