本文整理汇总了Scala中java.util.concurrent.CyclicBarrier类的典型用法代码示例。如果您正苦于以下问题:Scala CyclicBarrier类的具体用法?Scala CyclicBarrier怎么用?Scala CyclicBarrier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CyclicBarrier类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TestBarrierTimeoutException
//设置package包名称以及导入依赖的类
package akka.testkit
import scala.concurrent.duration.Duration
import java.util.concurrent.{ CyclicBarrier, TimeUnit, TimeoutException }
import akka.actor.ActorSystem
import scala.concurrent.duration.FiniteDuration
class TestBarrierTimeoutException(message: String) extends RuntimeException(message)
object TestBarrier {
val DefaultTimeout = Duration(5, TimeUnit.SECONDS)
def apply(count: Int) = new TestBarrier(count)
}
class TestBarrier(count: Int) {
private val barrier = new CyclicBarrier(count)
def await()(implicit system: ActorSystem): Unit = await(TestBarrier.DefaultTimeout)
def await(timeout: FiniteDuration)(implicit system: ActorSystem) {
try {
barrier.await(timeout.dilated.toNanos, TimeUnit.NANOSECONDS)
} catch {
case e: TimeoutException ?
throw new TestBarrierTimeoutException("Timeout of %s and time factor of %s"
format (timeout.toString, TestKitExtension(system).TestTimeFactor))
}
}
def reset(): Unit = barrier.reset()
}
示例2: ChanTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.util
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import java.util.concurrent.CyclicBarrier
import scala.collection.mutable.Buffer
import java.util.concurrent.CountDownLatch
@RunWith(classOf[JUnitRunner])
class ChanTest extends FunSuite {
test("Proc should admit one at a time, in the order received, queueing items") {
val threads = Buffer[Thread]()
val l = new CountDownLatch(1)
val b = new CyclicBarrier(2)
val p = Proc[Thread] { t => threads += t; l.countDown(); b.await() }
val t0 = new Thread {
override def run() {
p ! this
}
}
val t1 = new Thread {
override def run() {
l.await()
p ! this
b.await()
b.await()
}
}
t0.start();
t1.start()
t0.join();
t1.join()
assert(threads.toSeq == Seq(t0, t1))
}
test("Proc should swallow exceptions") {
val p = Proc[Int] { _ => throw new RuntimeException }
assert((p ! 4) ===((): Unit))
}
}
示例3: UpdaterTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.util
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import java.util.concurrent.{CyclicBarrier, CountDownLatch}
@RunWith(classOf[JUnitRunner])
class UpdaterTest extends FunSuite {
test("Prioritization") {
case class Work(p: Int)
@volatile var worked: Seq[Work] = Nil
val barrier = new CyclicBarrier(2)
val first = new CountDownLatch(1)
val u = new Updater[Work] {
protected def preprocess(elems: Seq[Work]) =
Seq(elems.minBy(_.p))
def handle(w: Work) {
worked :+= w
first.countDown()
barrier.await()
()
}
}
val w0 = Work(0)
val thr = new Thread("Test-Updater") {
override def run() {
u(w0)
}
}
thr.start()
first.await()
assert(worked == Seq(Work(0)))
u(Work(3))
u(Work(10))
u(Work(1))
u(Work(3))
barrier.await()
barrier.await()
thr.join()
assert(worked == Seq(Work(0), Work(1)))
}
}
示例4: ChanSpec
//设置package包名称以及导入依赖的类
package com.twitter.finagle.util
import org.specs.SpecificationWithJUnit
import java.util.concurrent.CyclicBarrier
import scala.collection.mutable.Buffer
import java.util.concurrent.CountDownLatch
class ChanSpec extends SpecificationWithJUnit {
"Proc" should {
"admit one at a time, in the order received, queueing items" in {
val threads = Buffer[Thread]()
val l = new CountDownLatch(1)
val b = new CyclicBarrier(2)
val p = Proc[Thread] { t => threads += t; l.countDown(); b.await() }
val t0 = new Thread {
override def run() {
p ! this
}
}
val t1 = new Thread {
override def run() {
l.await()
p ! this
b.await()
b.await()
}
}
t0.start(); t1.start()
t0.join(); t1.join()
threads.toSeq mustEqual Seq(t0, t1)
}
}
}