Class

Queue

Inheritance
< Object

This class provides a way to synchronize communication between threads.

Example:

  require 'thread'

  queue = Queue.new

  producer = Thread.new do
    5.times do |i|
      sleep rand(i) # simulate expense
      queue << i
      puts "#{i} produced"
    end
  end

  consumer = Thread.new do
    5.times do |i|
      value = queue.pop
      sleep rand(i/2) # simulate expense
      puts "consumed #{value}"
    end
  end

  consumer.join

Methods

Class

Visibility Signature
public new ()

Instance

Visibility Signature
public << (obj)
public clear ()
public deq (non_block=false)
public empty? ()
public enq (obj)
public length ()
public num_waiting ()
public pop (non_block=false)
public push (obj)
public shift (non_block=false)
public size ()

Class Method Detail

new()

Creates a new queue.

Instance Method Detail

<<(obj)

Alias for push

clear()

Removes all objects from the queue.

deq(non_block=false)

Alias for pop

empty?()

Returns true is the queue is empty.

enq(obj)

Alias for push

length()

Returns the length of the queue.

num_waiting()

Returns the number of threads waiting on the queue.

pop(non_block=false)

Retrieves data from the queue. If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block is true, the thread isn‘t suspended, and an exception is raised.

push(obj)

Pushes obj to the queue.

shift(non_block=false)

Alias for pop

size()

Alias for length