说我有一个Stream
,计算起来相当昂贵。我可以通过编写类似
import scala.actors.Futures._
val s = future { stream.size }
如果我抛弃对此Future
的引用,该线程是否会被垃圾收集器杀掉?
答案 0 :(得分: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
}
}