加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

当Scala“未来”被垃圾收集时会发生什么?

发布时间:2020-12-16 09:18:30 所属栏目:安全 来源:网络整理
导读:说我有一个流计算相当昂贵.我可以很容易地创建一个线程,“通过写”来计算前方“ import scala.actors.Futures._val s = future { stream.size } 如果我丢弃对未来的引用,这个线程会被垃圾收集器杀死吗? 解决方法 不.线程属于调度程序.在任何情况下,调度程序
说我有一个流计算相当昂贵.我可以很容易地创建一个线程,“通过写”来计算前方“

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

如果我丢弃对未来的引用,这个线程会被垃圾收集器杀死吗?

解决方法

不.线程属于调度程序.在任何情况下,调度程序都引用了未完成的未完成(这在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
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读