java.util.concurrent.LinkedBlockingDeque Scala Examples

The following examples show how to use java.util.concurrent.LinkedBlockingDeque. 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: AwtApp.scala    From scalaz-reactive   with Apache License 2.0 5 votes vote down vote up
package scalaz.reactive.examples.awt
import java.awt.event._
import java.util.EventObject
import java.util.concurrent.LinkedBlockingDeque

import javax.swing.JFrame
import javax.swing.SwingUtilities.invokeLater
import scalaz.reactive.Sink.Sink
import scalaz.reactive.{ Event, Reactive, Sink, Time }
import scalaz.zio.{ IO, Promise, RTS }

object AwtApp extends RTS {

  def run(app: => AwtApp): IO[Void, Unit] = {

    val io: IO[Void, IO[Void, Unit]] = for {
      promise <- Promise.make[Void, IO[Void, Unit]]
      _ <- IO.sync {
            invokeLater(() => {
              val sink = app.sinkIO
              unsafeRun(promise.complete(sink)) // TODO: see how to pass this IO differently
              ()
            })
          }.fork
      _ = println("started ")
      r <- promise.get
    } yield r
    IO.flatten(io)
  }
}
abstract class AwtApp(val name: String)
    extends JFrame
    with KeyListener
    with MouseListener
    with ActionListener {

  // implementing classes need to define
  def sink: Sink[EventObject]

  def eventsFilter(e: Event[EventObject]): Event[EventObject] = e

  // No need to have different queues, but for testing purposes let's see how merging events works
  private val mouseQueue = new LinkedBlockingDeque[MouseEvent]()
  private val keyQueue   = new LinkedBlockingDeque[KeyEvent]()

  private val actionQueue = new LinkedBlockingDeque[ActionEvent]()

  private def keyEvent: Event[KeyEvent] =
    Event(for {
      t <- Time.now
      e <- IO.sync(keyQueue.take())
    } yield (t, Reactive(e, keyEvent)))

  private def mouseEvent: Event[MouseEvent] =
    Event(for {
      t <- Time.now
      e <- IO.sync(mouseQueue.take())
    } yield (t, Reactive(e, mouseEvent)))

  private def actionEvent: Event[ActionEvent] =
    Event(for {
      t <- Time.now
      e <- IO.sync(actionQueue.take())
    } yield (t, Reactive(e, actionEvent)))

  private val allEvent = mouseEvent.merge(keyEvent).merge(actionEvent)

  private def offerEvent[A <: EventObject](e: A, q: java.util.Queue[A]): Unit = {
    q.add(e); ()
  }

  // key events
  override def keyPressed(e: KeyEvent): Unit  = offerEvent(e, keyQueue)
  override def keyReleased(e: KeyEvent): Unit = offerEvent(e, keyQueue)
  override def keyTyped(e: KeyEvent): Unit    = offerEvent(e, keyQueue)

  // mouse events
  override def mouseClicked(e: MouseEvent): Unit =
    offerEvent(e, mouseQueue)
  override def mousePressed(e: MouseEvent): Unit =
    offerEvent(e, mouseQueue)
  override def mouseReleased(e: MouseEvent): Unit =
    offerEvent(e, mouseQueue)
  override def mouseEntered(e: MouseEvent): Unit =
    offerEvent(e, mouseQueue)
  override def mouseExited(e: MouseEvent): Unit =
    offerEvent(e, mouseQueue)

  // action events
  def actionPerformed(e: ActionEvent): Unit = offerEvent(e, actionQueue)

  def sinkIO = Sink.sinkE(sink, eventsFilter(allEvent))

} 
Example 2
Source File: UnboundedInverseControlAwareDequeBasedMailbox.scala    From ForestFlow   with Apache License 2.0 5 votes vote down vote up
package ai.forestflow.akka

import java.util
import java.util.concurrent.LinkedBlockingDeque

import akka.actor.{ActorRef, ActorSystem}
import akka.dispatch._
import com.typesafe.config.Config


object UnboundedInverseControlAwareDequeBasedMailbox {

  
  trait InverseControlAwareMessageQueueSemantics extends QueueBasedMessageQueue {
    def controlQueue: util.Queue[Envelope]
    def queue: util.Queue[Envelope]

    def enqueue(receiver: ActorRef, handle: Envelope): Unit = handle match {
      case envelope @ Envelope(_: ControlMessage, _) ⇒ controlQueue add envelope
      case envelope                                  ⇒ queue add envelope
    }

    def dequeue(): Envelope = {
      val queueMsg = queue.poll()

      if (queueMsg ne null) queueMsg
      else controlQueue.poll()
    }

    override def numberOfMessages: Int = controlQueue.size() + queue.size()

    override def hasMessages: Boolean = !(queue.isEmpty && controlQueue.isEmpty)
  }

  // The message queue implementation
  class PriorityPersistenceAwareMessageQueue extends MessageQueue with UnboundedDequeBasedMessageQueueSemantics with InverseControlAwareMessageQueueSemantics {
    final val priorityMessages = new LinkedBlockingDeque[Envelope](1000)
    final val opportunisticMessages = new LinkedBlockingDeque[Envelope](1000)

    final val controlQueue: util.Queue[Envelope] = priorityMessages

    final val queue: util.Queue[Envelope] = opportunisticMessages

    override def enqueueFirst(receiver: ActorRef, handle: Envelope): Unit = handle match {
      case envelope @ Envelope(_: ControlMessage, _) ⇒ priorityMessages addFirst envelope
      case envelope                                  ⇒ opportunisticMessages addFirst envelope
    }
  }
}

class UnboundedInverseControlAwareDequeBasedMailbox extends MailboxType
  with ProducesMessageQueue[UnboundedControlAwareDequeBasedMailbox.PriorityPersistenceAwareMessageQueue] {

  import UnboundedControlAwareDequeBasedMailbox._

  // This constructor signature must exist, it will be called by Akka
  def this(settings: ActorSystem.Settings, config: Config) = {
    // put your initialization code here
    this()
  }

  final override def create(owner: Option[ActorRef], system: Option[ActorSystem]): MessageQueue = {
    new PriorityPersistenceAwareMessageQueue()
  }
} 
Example 3
Source File: UnboundedControlAwareDequeBasedMailbox.scala    From ForestFlow   with Apache License 2.0 5 votes vote down vote up
package ai.forestflow.akka

import java.util
import java.util.concurrent.LinkedBlockingDeque

import akka.actor.{ActorRef, ActorSystem}
import akka.dispatch._
import com.typesafe.config.Config

import scala.concurrent.duration.Duration

object UnboundedControlAwareDequeBasedMailbox {

  // The message queue implementation
  class PriorityPersistenceAwareMessageQueue extends MessageQueue with BoundedDequeBasedMessageQueueSemantics with ControlAwareMessageQueueSemantics {
    final val priorityMessages = new LinkedBlockingDeque[Envelope]()
    final val opportunisticMessages = new LinkedBlockingDeque[Envelope]()

    final val controlQueue: util.Queue[Envelope] = priorityMessages

    final val queue: util.Queue[Envelope] = opportunisticMessages

    override def enqueueFirst(receiver: ActorRef, handle: Envelope): Unit = handle match {
      case envelope @ Envelope(_: ControlMessage, _) ⇒ priorityMessages addFirst envelope
      case envelope                                  ⇒ opportunisticMessages addFirst envelope
    }

    override def pushTimeOut: Duration = ???
  }
}

class UnboundedControlAwareDequeBasedMailbox extends MailboxType
  with ProducesMessageQueue[UnboundedControlAwareDequeBasedMailbox.PriorityPersistenceAwareMessageQueue] {

  import UnboundedControlAwareDequeBasedMailbox._

  // This constructor signature must exist, it will be called by Akka
  def this(settings: ActorSystem.Settings, config: Config) = {
    // put your initialization code here
    this()
  }

  final override def create(owner: Option[ActorRef], system: Option[ActorSystem]): MessageQueue = {
    new PriorityPersistenceAwareMessageQueue()
  }
} 
Example 4
Source File: TestDeleteTopicsConcurrently.scala    From ohara   with Apache License 2.0 4 votes vote down vote up
package oharastream.ohara.configurator

import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger}
import java.util.concurrent.{ArrayBlockingQueue, Executors, LinkedBlockingDeque, TimeUnit}

import oharastream.ohara.client.configurator.{BrokerApi, TopicApi}
import oharastream.ohara.common.setting.TopicKey
import oharastream.ohara.common.util.Releasable
import oharastream.ohara.testing.WithBrokerWorker
import org.junit.{After, Test}
import org.scalatest.matchers.should.Matchers._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.jdk.CollectionConverters._

class TestDeleteTopicsConcurrently extends WithBrokerWorker {
  private[this] val configurator =
    Configurator.builder.fake(testUtil.brokersConnProps, testUtil().workersConnProps()).build()

  private[this] val topicApi = TopicApi.access.hostname(configurator.hostname).port(configurator.port)

  private[this] def result[T](f: Future[T]): T = Await.result(f, Duration(20, TimeUnit.SECONDS))

  private[this] val brokerClusterInfo = result(
    BrokerApi.access.hostname(configurator.hostname).port(configurator.port).list()
  ).head

  @Test
  def test(): Unit = {
    val loopMax       = 10
    val count         = 3
    val topicKeyQueue = new ArrayBlockingQueue[TopicKey](count)
    (0 until count).foreach(i => topicKeyQueue.put(TopicKey.of("test", i.toString)))
    val executors      = Executors.newFixedThreadPool(count)
    val exceptionQueue = new LinkedBlockingDeque[Throwable]()
    val closed         = new AtomicBoolean(false)
    val loopCount      = new AtomicInteger(0)
    (0 until count).foreach(
      _ =>
        executors.execute { () =>
          while (!closed.get() && loopCount.getAndIncrement() <= loopMax) try {
            val topicKey = topicKeyQueue.take()
            try result(
              topicApi.request
                .group(topicKey.group())
                .name(topicKey.name())
                .brokerClusterKey(brokerClusterInfo.key)
                .numberOfPartitions(1)
                .numberOfReplications(1)
                .create()
                .flatMap(_ => topicApi.start(topicKey))
                .flatMap { _ =>
                  TimeUnit.SECONDS.sleep(1)
                  topicApi.stop(topicKey)
                }
                .flatMap { _ =>
                  TimeUnit.SECONDS.sleep(1)
                  topicApi.delete(topicKey)
                }
            )
            finally topicKeyQueue.put(topicKey)
          } catch {
            case t: Throwable =>
              exceptionQueue.put(t)
              closed.set(true)
          }
        }
    )
    executors.shutdown()
    withClue(s"${exceptionQueue.asScala.map(_.getMessage).mkString(",")}") {
      executors.awaitTermination(60, TimeUnit.SECONDS) shouldBe true
    }
    exceptionQueue.size() shouldBe 0
  }
  @After
  def tearDown(): Unit = Releasable.close(configurator)
} 
Example 5
Source File: EventWriter.scala    From BigDL   with Apache License 2.0 4 votes vote down vote up
package com.intel.analytics.bigdl.visualization.tensorboard

import java.net.InetAddress
import java.util.concurrent.{LinkedBlockingDeque, TimeUnit}

import org.apache.hadoop.fs.{FileSystem, Path}
import org.tensorflow.util.Event


private[bigdl] class EventWriter(logDir: String,
                                 flushMillis: Int = 1000,
                                 fs: FileSystem) extends Runnable {
  private val eventQueue = new LinkedBlockingDeque[Event]()
  private val outputFile = new Path(logDir +
    s"/bigdl.tfevents.${(System.currentTimeMillis() / 1e3).toInt}" +
    s".${InetAddress.getLocalHost().getHostName()}")
  private val recordWriter = new RecordWriter(outputFile, fs)
  // Add an empty Event to the queue.
  eventQueue.add(Event.newBuilder().setWallTime(System.currentTimeMillis() / 1e3).build())
  @volatile private var running: Boolean = true

  def addEvent(event: Event): this.type = {
    eventQueue.add(event)
    this
  }

  private def flush(): this.type = {
    while (!eventQueue.isEmpty) {
      recordWriter.write(eventQueue.pop())
    }
    this
  }

  private def writeEvent(): this.type = {
    val e = eventQueue.poll(flushMillis, TimeUnit.MILLISECONDS)
    if (null != e) recordWriter.write(e)
    this
  }

  def close(): this.type = {
    running = false
    this
  }

  override def run(): Unit = {
    while (running) {
      writeEvent()
    }
    flush()
    recordWriter.close()
  }
}