Class: Threach::Queue

Inherits:
ArrayBlockingQueue
  • Object
show all
Defined in:
lib/jruby_threach.rb

Overview

An ArrayBlockingQueue with reasonable defaults for timeouts.

Constant Summary collapse

MS =
TimeUnit::MILLISECONDS

Instance Method Summary collapse

Constructor Details

#initialize(size = 5, timeout_in_ms = 5) ⇒ Queue

Create a new queue

Parameters:

  • size (Integer) (defaults to: 5)

    The size of the queue

  • timeout_in_ms (Integer) (defaults to: 5)

    How long to wait when trying to push or pop



30
31
32
33
# File 'lib/jruby_threach.rb', line 30

def initialize (size=5, timeout_in_ms = 5)
  super(size)
  @timeout = timeout_in_ms
end

Instance Method Details

#popObject?

Pop an object ouf of the queue

Returns:

  • (Object, nil)

    nil if it times out; the popped object otherwise



44
45
46
# File 'lib/jruby_threach.rb', line 44

def pop
  self.poll @timeout, MS
end

#push(obj) ⇒ Boolean

Try to add an object to the queue

Parameters:

  • obj (Object)

    The object to push

Returns:

  • (Boolean)

    true on success, false on timeout



38
39
40
# File 'lib/jruby_threach.rb', line 38

def push obj
  self.offer obj, @timeout, MS
end