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


Scala Lock类代码示例

本文整理汇总了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()
      }
    }
  }

} 
开发者ID:gitter-badger,项目名称:artemisia,代码行数:43,代码来源:TestEnv.scala

示例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)
} 
开发者ID:Hydrospheredata,项目名称:mist,代码行数:21,代码来源:RwLock.scala


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