Sco*_*son 14 garbage-collection scala future
说我有一个Stream相当昂贵的计算.我可以通过编写类似的东西轻松创建一个"提前计算"的线程
import scala.actors.Futures._
val s = future { stream.size }
Run Code Online (Sandbox Code Playgroud)
如果我然后抛弃对此的引用,该Future线程是否会被垃圾收集器杀掉?
ret*_*nym 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
}
}
Run Code Online (Sandbox Code Playgroud)