本文整理汇总了Scala中java.util.concurrent.locks.Lock类的典型用法代码示例。如果您正苦于以下问题:Scala Lock类的具体用法?Scala Lock怎么用?Scala Lock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Lock类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TestEnv
//设置package包名称以及导入依赖的类
package tech.artemisia.core
import java.util.concurrent.locks.{ReentrantLock, Lock}
import scala.collection.mutable
object TestEnv extends Env {
override val osUtil: OSUtil = new TestOsUtil
class TestOsUtil extends OSUtil {
private[this] val systemVariableMap: mutable.Map[String,String] = mutable.Map("foo" -> "bar")
private[this] val systemPropertiesMap: mutable.Map[String,String] = mutable.Map("user.home" -> System.getProperty("user.home"))
private[this] val lock :Lock = new ReentrantLock()
override def getSystemVariable(variable: String): Option[String] = {
systemVariableMap get variable
}
def getSystemProperties(variable: String): Option[String] = {
systemPropertiesMap get variable
}
def withSysVar(map: scala.collection.Map[String,String])(body : => Unit): Unit = {
try {
lock.lock()
systemVariableMap.clear()
systemVariableMap ++= map.toSeq
body
}
catch {
case e: Throwable => throw e
}
finally {
lock.unlock()
}
}
}
}
示例2: 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)
}