English 中文(简体)
在方案终止之前,未来不再发生。
原标题:Futures do not run before program termination

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")
    }
}

Instead of printing:

Test print before future
Hello future!
Test print after future

它只是印刷品:

Test print before future
Test print after future

我为什么有这种行为的想法? 我的Scala汇编者版本为2.10.0-20120507。

最佳回答

问题是,作为独立的方案,如果主要读物是在工人的校对之一之前终止,你可以执行“Hello Futurecode> 印本。 (新的未来图书馆间谍系 da子)

您也可使用<代码>Await的物体(也载于scala.con Current),等到未来f完成:

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)
  }
}

This can print:

Test print before future
Test print after future
Hello future!

或者,它可以根据阅读时间表印刷“Hello Future”和“以后的试验”。

同样,你也可以强迫主线等到<代码>f>上填满最后l:

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")
  }
}

Which would print:

Test print before future
Hello future!
Test print after future

然而,请注意,在您使用<代码>Await时,您将再次加以阻挡。 当然,这是为了确保你的主要应用方式不会终止,但除非另有必要,否则一般不应使用。

(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.)

问题回答

我认为,这里的问题是时机。 很可能,你未来的法典正在单独进行 de。 我认为,申请进展非常快,而这种 de魔的read光没有足够的时间加以适当执行(申请不会等待 de子的完结)。 但这也正是基于系统的行为。 对我来说,它印有:

Test print before future
Test print after future
Hello future!

and then exits (I m using Scala 2.10.0-M3). You can try following in order to test it - just put main execution thread in sleep for several seconds and see whether Hello future! is printed:

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")

        Thread.sleep(3000) 
        println("Test print at the end.")
    }
}

我只想补充一点,总的来说,还有另一个可能性是,未来不会运行: 点燃read池的极限。

在你看来,这很可能像其他人所指出的,只是一个时间问题,但作为未来参考,我们认为:

import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration


object FutureDebug {
  def main( args: Array[String] ) {

    for (i <- Range(0, 4)) {
      future {
        while (true) {
          Thread.sleep(1000)
          println("I m doing stupid things in a future")
        }
      }
    }

    println("(1) reached? yes")
    val fut = future {
      for (i <- Range(0, 1000)) {
        println("never reached " + i)
      }
      3.14
    }    
    println("(2) reached? yes")
    Await.result(fut, Duration.Inf)
    println("(3) reached? no")
  }
}

在我的机器上,缺省的全球执行环境只有4条路面。 由于工人的read子忙于4个非专利的未来,以下未来将永远无法运行。 因此,在处理多个(实时)长期的未来时,应谨慎对待缺省执行情形,最好在





相关问题
Need help with web application settings [closed]

Pls give me the solutions for this two problem, which i was asked recently in interview. Prob1: Suppose I have application with 10 admin, so they can change the data anytime their having own userid ...

AutoResetEvent, ManualResetEvent vs Monitor

Lets say I have to orchestrate a synchronization algorithm in .Net 3.5 SP1 and any of the synchronization primitives listed in the title fit perfectly for the task. From a performance perspective, is ...

PHP and Concurrency

I ve been doing some web development work in PHP recently which has led me to study up on the language in general. So far I have not needed to use it to interact with a database, but I know it ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

Prevent Concurrent Editing of a List Item

In Sharepoint MOSS multiple users can edit the same item in a sharepoint list at the same time…the first person to save their edit “wins”. Is there a way to prevent this, to lock the list item while ...

How to properly catch RuntimeExceptions from Executors?

Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if myRunnable throws a RuntimeExcpetion, how can I catch it? ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...

热门标签