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


Scala CyclicBarrier类代码示例

本文整理汇总了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()
} 
开发者ID:love1314sea,项目名称:akka-2.3.16,代码行数:34,代码来源:TestBarrier.scala

示例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))
  }

} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:48,代码来源:ChanTest.scala

示例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)))
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:49,代码来源:UpdaterTest.scala

示例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)
    }
  }
} 
开发者ID:deenar,项目名称:fintest,代码行数:39,代码来源:ChanSpec.scala


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