• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala CyclicBarrier类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Vectors类代码示例发布时间:2022-05-23
下一篇:
Scala FrontendAuditFilter类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap