本文整理汇总了Scala中scala.ref.WeakReference类的典型用法代码示例。如果您正苦于以下问题:Scala WeakReference类的具体用法?Scala WeakReference怎么用?Scala WeakReference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WeakReference类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: WeakExpressionWrapper
//设置package包名称以及导入依赖的类
package com.avsystem.scex
package compiler
import scala.ref.WeakReference
import scala.util.Try
private class WeakExpressionWrapper(exprDef: ExpressionDef, initiallyWrapped: RawExpression) extends RawExpression {
var expressionRef = new WeakReference(initiallyWrapped)
private def wrappedExpression: RawExpression =
expressionRef.get match {
case Some(expr) => expr
case None =>
expressionRef = new WeakReference(actuallyCompileExpression(exprDef).get)
wrappedExpression
}
def apply(context: ExpressionContext[_, _]) =
wrappedExpression.apply(context)
def debugInfo =
wrappedExpression.debugInfo
}
private def actuallyCompileExpression(exprDef: ExpressionDef) =
super.compileExpression(exprDef)
override protected def compileExpression(exprDef: ExpressionDef): Try[RawExpression] =
actuallyCompileExpression(exprDef).map(new WeakExpressionWrapper(exprDef, _))
}
示例2: ActorSystems
//设置package包名称以及导入依赖的类
package akkaviz.events
import akka.actor.{ActorPath, ActorRef, ActorSystem}
import akka.viz.ActorCellInstrumentation
import scala.collection.breakOut
import scala.ref.WeakReference
import scala.util.Try
object ActorSystems {
private[this] val systemReferences = scala.collection.mutable.Map[String, WeakReference[ActorSystem]]()
def systems: scala.collection.immutable.Map[String, ActorSystem] = systemReferences.flatMap {
case (name, ref) => ref.get.map {
system => name -> system
}
}(breakOut)
def registerSystem(system: ActorSystem): Unit = {
systemReferences.update(system.name, WeakReference(system))
}
def tell(path: String, message: Any): Unit = {
Try {
val actorPath = ActorPath.fromString(path)
systems.get(actorPath.address.system).foreach {
system =>
system.actorSelection(actorPath).tell(message, ActorRef.noSender)
}
}
}
def refreshActorState(path: String): Unit = {
tell(path, ActorCellInstrumentation.RefreshInternalStateMsg)
}
}