本文整理汇总了Scala中java.util.concurrent.locks.ReentrantReadWriteLock类的典型用法代码示例。如果您正苦于以下问题:Scala ReentrantReadWriteLock类的具体用法?Scala ReentrantReadWriteLock怎么用?Scala ReentrantReadWriteLock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ReentrantReadWriteLock类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ConsensusRouteSpec
//设置package包名称以及导入依赖的类
package com.wavesplatform.http
import java.util.concurrent.locks.ReentrantReadWriteLock
import com.wavesplatform.BlockGen
import com.wavesplatform.history.HistoryWriterImpl
import com.wavesplatform.http.ApiMarshallers._
import com.wavesplatform.settings.FunctionalitySettings
import com.wavesplatform.state2._
import com.wavesplatform.state2.reader.StateReader
import org.scalamock.scalatest.MockFactory
import org.scalatest.prop.PropertyChecks
import play.api.libs.json.JsObject
import scorex.api.http.BlockNotExists
import scorex.consensus.nxt.api.http.NxtConsensusApiRoute
import scorex.crypto.encode.Base58
class ConsensusRouteSpec extends RouteSpec("/consensus") with RestAPISettingsHelper with PropertyChecks with MockFactory with BlockGen with HistoryTest {
private val state = mock[StateReader]
private val history = HistoryWriterImpl(None, new ReentrantReadWriteLock()).get
appendGenesisBlock(history)
for (i <- 1 to 10) appendTestBlock(history)
private val route = NxtConsensusApiRoute(restAPISettings, state, history, FunctionalitySettings.TESTNET).route
routePath("/generationsignature") - {
"for last block" in {
Get(routePath("/generationsignature")) ~> route ~> check {
(responseAs[JsObject] \ "generationSignature").as[String] shouldEqual Base58.encode(history.lastBlock.get.consensusData.generationSignature)
}
}
"for existed block" in {
val block = history.blockAt(3).get
Get(routePath(s"/generationsignature/${block.uniqueId.base58}")) ~> route ~> check {
(responseAs[JsObject] \ "generationSignature").as[String] shouldEqual Base58.encode(block.consensusData.generationSignature)
}
}
"for not existed block" in {
Get(routePath(s"/generationsignature/brggwg4wg4g")) ~> route should produce(BlockNotExists)
}
}
routePath("/basetarget") - {
"for existed block" in {
val block = history.blockAt(3).get
Get(routePath(s"/basetarget/${block.uniqueId.base58}")) ~> route ~> check {
(responseAs[JsObject] \ "baseTarget").as[Long] shouldEqual block.consensusData.baseTarget
}
}
"for not existed block" in {
Get(routePath(s"/basetarget/brggwg4wg4g")) ~> route should produce(BlockNotExists)
}
}
}
示例2: newState
//设置package包名称以及导入依赖的类
package com.wavesplatform.state2
import java.util.concurrent.locks.ReentrantReadWriteLock
import com.wavesplatform.history.HistoryWriterImpl
import com.wavesplatform.settings.FunctionalitySettings
import com.wavesplatform.state2.reader.{CompositeStateReader, StateReader}
import scorex.block.Block
import scorex.settings.TestFunctionalitySettings
import scorex.transaction.{History, ValidationError}
package object diffs {
private val lock = new ReentrantReadWriteLock()
def newState(): StateWriterImpl = new StateWriterImpl(StateStorage(None, dropExisting = false).get, lock)
def newHistory(): History = HistoryWriterImpl(None, lock).get
val ENOUGH_AMT: Long = Long.MaxValue / 3
def assertDiffEi(preconditions: Seq[Block], block: Block, fs: FunctionalitySettings = TestFunctionalitySettings.Enabled)(assertion: Either[ValidationError, BlockDiff] => Unit): Unit = {
val differ: (StateReader, Block) => Either[ValidationError, BlockDiff] = (s, b) => BlockDiffer.fromBlock(fs, s, None)(b)
val state = newState()
preconditions.foreach { precondition =>
val preconditionDiff = differ(state, precondition).explicitGet()
state.applyBlockDiff(preconditionDiff)
}
val totalDiff1 = differ(state, block)
assertion(totalDiff1)
val preconditionDiff = BlockDiffer.unsafeDiffMany(fs, newState(), None)(preconditions)
val compositeState = new CompositeStateReader(newState(), preconditionDiff)
val totalDiff2 = differ(compositeState, block)
assertion(totalDiff2)
}
def assertDiffAndState(preconditions: Seq[Block], block: Block, fs: FunctionalitySettings = TestFunctionalitySettings.Enabled)(assertion: (BlockDiff, StateReader) => Unit): Unit = {
val differ: (StateReader, Block) => Either[ValidationError, BlockDiff] = (s, b) => BlockDiffer.fromBlock(fs, s, None)(b)
val state = newState()
preconditions.foreach { precondition =>
val preconditionDiff = differ(state, precondition).explicitGet()
state.applyBlockDiff(preconditionDiff)
}
val totalDiff1 = differ(state, block).explicitGet()
state.applyBlockDiff(totalDiff1)
assertion(totalDiff1, state)
val preconditionDiff = BlockDiffer.unsafeDiffMany(fs, newState(), None)(preconditions)
val compositeState = new CompositeStateReader(newState(), preconditionDiff)
val totalDiff2 = differ(compositeState, block).explicitGet()
assertion(totalDiff2, new CompositeStateReader(compositeState, totalDiff2))
}
def produce(errorMessage: String): ProduceError = new ProduceError(errorMessage)
}
示例3: StateReaderEffectiveBalanceTest
//设置package包名称以及导入依赖的类
package com.wavesplatform.state2.reader
import java.util.concurrent.locks.ReentrantReadWriteLock
import com.wavesplatform.state2.StateStorage
import com.wavesplatform.state2.StateStorage._
import org.scalatest.{Matchers, Outcome, fixture}
import scorex.account.Address
class StateReaderEffectiveBalanceTest extends fixture.FunSuite with Matchers {
val acc: Address = Address.fromPublicKey(Array.emptyByteArray)
val stateHeight = 100
override type FixtureParam = StateStorage
override protected def withFixture(test: OneArgTest): Outcome = {
val storage = StateStorage(None, dropExisting = false).get
storage.setHeight(stateHeight)
test(storage)
}
test("exposes minimum of all 'current' and one 'previous' of oldest record") { storage =>
storage.balanceSnapshots.put(accountIndexKey(acc, 20), (0, 0, 1))
storage.balanceSnapshots.put(accountIndexKey(acc, 75), (20, 0, 200))
storage.balanceSnapshots.put(accountIndexKey(acc, 90), (75, 0, 100))
storage.lastBalanceSnapshotHeight.put(acc.bytes, 90)
new StateReaderImpl(storage, new ReentrantReadWriteLock()).effectiveBalanceAtHeightWithConfirmations(acc, stateHeight, 50) shouldBe 1
}
test("exposes current effective balance if no records in past N blocks are made") { storage =>
storage.balanceSnapshots.put(accountIndexKey(acc, 20), (0, 0, 1))
storage.portfolios.put(acc.bytes, (1, (0, 0), Map.empty))
storage.lastBalanceSnapshotHeight.put(acc.bytes, 20)
new StateReaderImpl(storage, new ReentrantReadWriteLock()).effectiveBalanceAtHeightWithConfirmations(acc, stateHeight, 50) shouldBe 1
}
test("doesn't include info older than N blocks") { storage =>
storage.balanceSnapshots.put(accountIndexKey(acc, 20), (0, 0, 1000))
storage.balanceSnapshots.put(accountIndexKey(acc, 50), (20, 0, 50000))
storage.balanceSnapshots.put(accountIndexKey(acc, 75), (50, 0, 100000))
storage.lastBalanceSnapshotHeight.put(acc.bytes, 75)
new StateReaderImpl(storage, new ReentrantReadWriteLock()).effectiveBalanceAtHeightWithConfirmations(acc, stateHeight, 50) shouldBe 50000
}
test("includes most recent update") { storage =>
storage.balanceSnapshots.put(accountIndexKey(acc, 20), (0, 0, 1000))
storage.balanceSnapshots.put(accountIndexKey(acc, 51), (20, 0, 50000))
storage.balanceSnapshots.put(accountIndexKey(acc, 100), (51, 0, 1))
storage.lastBalanceSnapshotHeight.put(acc.bytes, 100)
new StateReaderImpl(storage, new ReentrantReadWriteLock()).effectiveBalanceAtHeightWithConfirmations(acc, stateHeight, 50) shouldBe 1
}
}
示例4: StateWriterSpec
//设置package包名称以及导入依赖的类
package com.wavesplatform.state2
import java.util.concurrent.locks.ReentrantReadWriteLock
import org.scalacheck.Gen
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalatest.{FunSuite, Matchers}
class StateWriterSpec extends FunSuite with Matchers with GeneratorDrivenPropertyChecks {
test("increase height when applying block diff") {
val storage = StateStorage(None, dropExisting = false).get
val writer = new StateWriterImpl(storage, new ReentrantReadWriteLock())
forAll(Gen.choose(0, Int.MaxValue)) { heightDiff =>
val h = writer.height
writer.applyBlockDiff(BlockDiff(Diff.empty, heightDiff, Map.empty))
writer.height shouldBe h + heightDiff
storage.getHeight shouldBe h + heightDiff
}
}
}
示例5: withLock
//设置package包名称以及导入依赖的类
package io.hydrosphere.mist.master.data
import java.util.concurrent.locks.{Lock, ReentrantReadWriteLock}
trait RwLock {
private val rwLock = new ReentrantReadWriteLock()
protected def withLock[T](l: Lock, f: => T): T = {
l.lock()
try {
f
} finally {
l.unlock()
}
}
protected def withReadLock[T](f: => T): T = withLock(rwLock.readLock, f)
protected def withWriteLock[T](f: => T): T = withLock(rwLock.writeLock, f)
}
示例6: CachedValue
//设置package包名称以及导入依赖的类
package io.ahamdy.taskforce.common
import java.time.ZonedDateTime
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.locks.ReentrantReadWriteLock
import io.ahamdy.taskforce.syntax.zonedDateTime._
import cats.syntax.flatMap._
import fs2.interop.cats._
import fs2.Task
import scala.concurrent.duration.FiniteDuration
class CachedValue[A](source: Task[A], ttl: FiniteDuration, time: Time) {
val currentValue = new AtomicReference[Task[A]]()
val lastUpdated = new AtomicReference[ZonedDateTime](time.epoch)
val lock = new ReentrantReadWriteLock()
def value: Task[A] =
time.now.flatMap{ now =>
if (lastUpdated.get().isBefore(now.minus(ttl))){
try{
lock.writeLock().lock()
source.unsafeRunSync() match {
case Right(a) =>
Task.delay(lastUpdated.set(now)) >>
Task.delay {
currentValue.set(Task.now(a))
a
}
case Left(_) => source
}
}finally {
lock.writeLock().unlock()
}
}else{
try{
lock.readLock().lock()
currentValue.get()
}finally {
lock.readLock().unlock()
}
}
}
}
示例7: RWLock
//设置package包名称以及导入依赖的类
package org.rebeam.tree.server.util
import java.util.concurrent.locks.ReentrantReadWriteLock
class RWLock() {
private val lock: ReentrantReadWriteLock = new ReentrantReadWriteLock()
def write[T](w: =>T): T = {
lock.writeLock().lock()
try {
return w
} finally {
lock.writeLock().unlock()
}
}
def read[T](r: =>T): T = {
lock.readLock().lock()
try {
return r
} finally {
lock.readLock().unlock()
}
}
}
object RWLock {
def apply() = new RWLock()
}