14

計算するのにStreamかなり費用がかかるとしましょう。次のように書くだけで、「先に計算する」スレッドを簡単に作成できます。

import scala.actors.Futures._
val s = future { stream.size }

次に、これへの参照を破棄Futureすると、そのスレッドはガベージコレクターによって強制終了されますか?

4

1 に答える 1

15

いいえ。スレッドはスケジューラに属しています。いずれにせよ、スケジューラは本体の未完成の Future (これは で発生しますa.start()) への参照を持っているため、完了前にガベージ コレクションは行われません。

object Futures {

  /** Arranges for the asynchronous execution of `body`,
   *  returning a future representing the result.
   *
   *  @param  body the computation to be carried out asynchronously
   *  @return      the future representing the result of the
   *               computation
   */
  def future[T](body: => T): Future[T] = {
    val c = new Channel[T](Actor.self(DaemonScheduler))
    val a = new FutureActor[T](_.set(body), c)
    a.start()
    a
  }
}
于 2010-07-12T06:02:48.203 に答える