Scala 此.synchronized是否可以保护您免受垃圾收集器的攻击?

Scala 此.synchronized是否可以保护您免受垃圾收集器的攻击?,scala,garbage-collection,Scala,Garbage Collection,我有一些代码使用了WeakReference。我不得不实施一个难看的解决方法来解决这个问题,但我想知道,仅仅添加一个this.synchronized是否可以解决我的垃圾收集器问题。这是代码,问题出在函数create /** * The canonical map. */ var unicityTable = new WeakHashMap[CanonicalType, LightWeightWrapper[CanonicalType]] with SynchronizedM

我有一些代码使用了
WeakReference
。我不得不实施一个难看的解决方法来解决这个问题,但我想知道,仅仅添加一个
this.synchronized
是否可以解决我的垃圾收集器问题。这是代码,问题出在函数
create

  /**
   * The canonical map.
   */
  var unicityTable = new WeakHashMap[CanonicalType, LightWeightWrapper[CanonicalType]] with SynchronizedMap[CanonicalType, LightWeightWrapper[CanonicalType]]

  /**
   * Create a new element from any object.
   * @param from the object that will be used to generate a new instance of your canonical object.
   */
  def create(from: FromType) = {
    val newElt = makeFrom(from)
      // I wonder if adding a this.synchronized here (and of course removing the test for garbage collection) might solve the problem more elegantly 
      val wrapper = unicityTable.get(newElt)
      wrapper match {
        case Some(w) => w._wrap.get match { // if the element is in the map
          case Some(element) => element // the element was not garbage collected while we obtaining it
          case None => // some how, the wrapped element was garbage collected before we get it, so we recreate it, and put it back
            unicityTable.put(newElt, LightWeightWrapper(newElt))
            newElt
        }
        case None => // the element is not in the map
          unicityTable.put(newElt, LightWeightWrapper(newElt))
          newElt
      }

  }

  class LightWeightWrapper[T <: AnyRef] private (wrap: T) {

    val _wrap = new WeakReference(wrap)

    def wrapped = _wrap.get match {
      case Some(w) => w
      case None => // should never happen 
        throw new IllegalStateException
    }

    override lazy val hashCode = wrapped.hashCode

    override def equals(o: Any): Boolean = o match {
      case LightWeightWrapper(w) => w eq this.wrapped
      case _ => false
    }
  }
/**
*规范映射。
*/
var unicityTable=new WeakHashMap[CanonicalType,LightWeightWrapper[CanonicalType]]与SynchronizedMap[CanonicalType,LightWeightWrapper[CanonicalType]]
/**
*从任何对象创建新元素。
*@param,该对象将用于生成规范对象的新实例。
*/
def create(from:FromType)={
val newElt=makeFrom(from)
//我想知道在这里添加this.synchronized(当然还有删除垃圾收集测试)是否可以更优雅地解决这个问题
val wrapper=unicityTable.get(newElt)
包装匹配{
case Some(w)=>w.\u wrap.get match{//如果元素在映射中
case Some(element)=>element//获取元素时,该元素未被垃圾收集
case None=>//如何,包装的元素在我们得到它之前被垃圾收集,所以我们重新创建它,并将其放回原处
unicityTable.put(newElt,LightWeightWrapper(newElt))
纽厄特
}
case None=>//元素不在映射中
unicityTable.put(newElt,LightWeightWrapper(newElt))
纽厄特
}
}
类LightWeightWrapper[T w
case None=>//不应该发生
抛出新的非法状态异常
}
重写lazy val hashCode=wrapped.hashCode
覆盖def等于(o:Any):布尔值=o匹配{
箱轻量包装(w)=>w等重包装
大小写=>false
}
}

所以问题是:垃圾收集是否会在同步块的执行期间停止?

否,垃圾收集仍然发生在
已同步的
内部。此外,
已被阻止进行垃圾收集,因为它位于堆栈上,因此您也不会从中获得任何帮助

我不知道你在做什么,但我想你可能真的想使用
SoftReference
而不是
WeakReference
。只有
WeakReference
引用的对象可以在所有硬引用消失后立即收集。只有
SoftReference
引用的对象可以保留到虚拟机内存不足