当前位置: 首页>>代码示例>>Scala>>正文


Scala ConcurrentModificationException类代码示例

本文整理汇总了Scala中java.util.ConcurrentModificationException的典型用法代码示例。如果您正苦于以下问题:Scala ConcurrentModificationException类的具体用法?Scala ConcurrentModificationException怎么用?Scala ConcurrentModificationException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ConcurrentModificationException类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: MapDBStorage

//设置package包名称以及导入依赖的类
package one.lockstep.util.storage.mapdb

import java.util.ConcurrentModificationException

import one.lockstep.util.storage.TransactionalStorage
import TransactionalStorage._
import org.mapdb.{DB, TxMaker}

class MapDBStorage(txMaker: TxMaker) extends TransactionalStorage {

  private[this] case class Schema(db: DB) {
    lazy val leaseCounter = db.atomicLong("lease-counter")
    lazy val data = db.namedMap("data")
    lazy val acquiredByLease = db.namedNumberMap("leases")
  }

  private lazy val boundStorage = {
    val boundStorage = txMaker.bind(db => Schema(db))
    // init
    boundStorage.tx { schema =>
      schema.leaseCounter
      schema.data
      schema.acquiredByLease
    }
    boundStorage
  }

  private def tx[A](f: Schema => A) = Lock.synchronized { boundStorage.tx(f) }
  private def ro[A](f: Schema => A) = boundStorage.ro(f)
  private def keyOf(coords: Coordinates): String = s"${coords.collectionName}.${coords.itemId}"

  
  @throws[ConcurrentModificationException]
  override def save(lease: Lease, view: View): Unit = tx { schema =>
    view.entries.foreach { case (coord, value) =>
      val key = keyOf(coord)
      val currentLease = schema.acquiredByLease.getOrElse(key, -1)
      if (lease.value != currentLease) throw new ConcurrentModificationException(s"obsolete lease for $coord")
      schema.acquiredByLease.put(key, lease.value)
      value match {
        case None => schema.data.remove(key) // we remove the data but keep the lease..
        case Some(bytes) => schema.data.put(key, bytes)
      }
    }
  }

  override def clear(): Unit = tx { schema =>
    schema.acquiredByLease.clear()
    schema.data.clear()
    schema.leaseCounter.set(0)
  }


}


private object Lock 
开发者ID:lockstep-one,项目名称:vault,代码行数:58,代码来源:MapDBStorage.scala


注:本文中的java.util.ConcurrentModificationException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。