java.util.concurrent.BlockingQueue Scala Examples
The following examples show how to use java.util.concurrent.BlockingQueue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: PublishToBlockingResource.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream import java.util.concurrent.{ArrayBlockingQueue, BlockingQueue} import akka.NotUsed import akka.actor.ActorSystem import akka.stream.DelayOverflowStrategy import akka.stream.scaladsl.{Flow, Sink, Source} import scala.concurrent.duration._ import scala.util.Failure object PublishToBlockingResource extends App { implicit val system = ActorSystem("PublishToBlockingResource") implicit val ec = system.dispatcher val slowSink: Sink[Seq[Int], NotUsed] = Flow[Seq[Int]] .delay(1.seconds, DelayOverflowStrategy.backpressure) .to(Sink.foreach(e => println(s"Reached sink: $e"))) val blockingResource: BlockingQueue[Int] = new ArrayBlockingQueue[Int](100) //Start a new `Source` from some (third party) blocking resource which can be opened, read and closed val source: Source[Int, NotUsed] = Source.unfoldResource[Int, BlockingQueue[Int]]( () => blockingResource, //open (q: BlockingQueue[Int]) => Some(q.take()),//read (_: BlockingQueue[Int]) => {}) //close val done = source .groupedWithin(10, 2.seconds) .watchTermination()((_, done) => done.onComplete { case Failure(err) => println(s"Flow failed: $err") case each => println(s"Server flow terminated: $each") }) .runWith(slowSink) //simulate n process that publish in blocking fashion to the queue (1 to 1000).par.foreach(value => blockingResource.put(value)) }
Example 2
Source File: DSEWriterThread.scala From spatial with MIT License | 5 votes |
package spatial.dse import java.io.PrintStream import java.util.concurrent.BlockingQueue case class DSEWriterThread( threadId: Int, spaceSize: BigInt, filename: String, header: String, workQueue: BlockingQueue[Array[String]] ) extends Runnable { private var isAlive: Boolean = true private var hasTerminated: Boolean = false def requestStop(): Unit = { isAlive = false } def run(): Unit = { val data = new PrintStream(filename) data.println(header + ",Timestamp") val P = BigDecimal(spaceSize) var N = BigDecimal(0) var nextNotify = BigDecimal(0); val notifyStep = 5000 val startTime = System.currentTimeMillis() while(isAlive) { try { val array = workQueue.take() if (array.nonEmpty) { array.foreach { line => data.println(line) } data.flush() N += array.length if (N > nextNotify) { val time = System.currentTimeMillis - startTime println(" %.4f".format(100 * (N / P).toFloat) + s"% ($N / $P) Complete after ${time / 1000} seconds") nextNotify += notifyStep } } else if (array.isEmpty) requestStop() // Somebody poisoned the work queue! } catch {case e: Throwable => println(e.getMessage) e.getStackTrace.foreach{line => println(" " + line) } requestStop() } } data.close() hasTerminated = true } }
Example 3
Source File: EventLoop.scala From Hive-JDBC-Proxy with Apache License 2.0 | 5 votes |
package com.enjoyyin.hive.proxy.jdbc.util import java.util.concurrent.BlockingQueue import java.util.concurrent.PriorityBlockingQueue import org.apache.hive.service.cli.HiveSQLException import scala.collection.JavaConversions._ abstract class EventLoop[E](override val threadName: String, val maxCapacity: Int) extends LoopThread with LoopCollection[E] { private val eventQueue: BlockingQueue[E] = new PriorityBlockingQueue[E](2 * maxCapacity + 1) protected override def doLoop = { val event = eventQueue.take onReceiveSafety(event) } def remove(obj: E): Unit = { eventQueue.remove(obj) } def remove(filter: E => Boolean): Unit = { eventQueue.filter(filter).foreach(eventQueue.remove) } def post(event: E): Unit = { if(eventQueue.size > maxCapacity) { throw new HiveSQLException(threadName + " have reached max capacity of queue!") } eventQueue.put(event) } def leftCapacity = maxCapacity - eventQueue.size }
Example 4
Source File: LeanMessagingProvider.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.connector.lean import java.util.concurrent.BlockingQueue import java.util.concurrent.LinkedBlockingQueue import scala.collection.mutable.Map import scala.collection.concurrent.TrieMap import scala.concurrent.duration.FiniteDuration import scala.util.Success import scala.util.Try import akka.actor.ActorSystem import org.apache.openwhisk.common.Logging import org.apache.openwhisk.core.WhiskConfig import org.apache.openwhisk.core.connector.MessageConsumer import org.apache.openwhisk.core.connector.MessageProducer import org.apache.openwhisk.core.connector.MessagingProvider import org.apache.openwhisk.core.entity.ByteSize val queues: Map[String, BlockingQueue[Array[Byte]]] = new TrieMap[String, BlockingQueue[Array[Byte]]] def getConsumer(config: WhiskConfig, groupId: String, topic: String, maxPeek: Int, maxPollInterval: FiniteDuration)( implicit logging: Logging, actorSystem: ActorSystem): MessageConsumer = { val queue = queues.getOrElseUpdate(topic, new LinkedBlockingQueue[Array[Byte]]()) new LeanConsumer(queue, maxPeek) } def getProducer(config: WhiskConfig, maxRequestSize: Option[ByteSize] = None)( implicit logging: Logging, actorSystem: ActorSystem): MessageProducer = new LeanProducer(queues) def ensureTopic(config: WhiskConfig, topic: String, topicConfigKey: String, maxMessageBytes: Option[ByteSize] = None)( implicit logging: Logging): Try[Unit] = { if (queues.contains(topic)) { Success(logging.info(this, s"topic $topic already existed")) } else { queues.put(topic, new LinkedBlockingQueue[Array[Byte]]()) Success(logging.info(this, s"topic $topic created")) } } }
Example 5
Source File: DefaultThreadPool.scala From scrapy4s with GNU Lesser General Public License v3.0 | 5 votes |
package com.scrapy4s.thread import java.util.concurrent.{BlockingQueue, CountDownLatch, Executor, TimeUnit} import java.util.concurrent.atomic.AtomicBoolean import com.scrapy4s.exception.QueueTimeOutException import org.slf4j.LoggerFactory class DefaultThreadPool( name: String, threadCount: Int, queue: BlockingQueue[Runnable] ) extends ThreadPool { val logger = LoggerFactory.getLogger(classOf[DefaultThreadPool]) val startFlag = new AtomicBoolean(false) val countDownLatch = new CountDownLatch(threadCount) init() private def init(): Unit = { if (startFlag.compareAndSet(false, true)) { (1 to threadCount).foreach(i => { val thread = new Thread(() => {task()}) thread.setName(s"pool-$name-$i") thread.start() }) } else { throw new Exception("线程池已经启动") } } def task() = { try { while (startFlag.get()) { try { val runnable = queue.poll(1, TimeUnit.SECONDS) if (runnable == null) { throw new QueueTimeOutException() } runnable.run() } catch { case _: QueueTimeOutException => case e: Exception => logger.error("thread pool exception", e) } } } finally { countDownLatch.countDown() } } override def shutdown() = { startFlag.compareAndSet(true, false) } override def waitForStop() = { countDownLatch.await() } override def waitForStop(timeout: Long, unit: TimeUnit): Boolean = { countDownLatch.await(timeout, unit) } override def execute(command: Runnable) = { if (command == null) throw new NullPointerException() queue.put(command) } }