Top Description Inners Fields Constructors Methods
org.jruby.ext.thread

public Class Queue

extends RubyObject
implements DataType
Class Inheritance
All Implemented Interfaces
org.jruby.runtime.marshal.DataType
Known Direct Subclasses
org.jruby.ext.thread.SizedQueue
Annotations
@JRubyClass
name:Queue
Imports
java.util.concurrent.BlockingQueue, .TimeUnit, java.util.concurrent.atomic.AtomicInteger, java.util.concurrent.locks.Condition, .ReentrantLock, org.jruby.Ruby, .RubyArray, .RubyBoolean, .RubyClass, .RubyFixnum, .RubyHash, .RubyMarshal, .RubyNumeric, .RubyObject, .RubyThread, org.jruby.anno.JRubyClass, .JRubyMethod, org.jruby.ast.util.ArgsUtil, org.jruby.exceptions.RaiseException, org.jruby.runtime.Helpers, .ThreadContext, .Visibility, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.marshal.DataType, org.jruby.util.TypeConverter

The "Queue" class from the 'thread' library. The implementation is a kernel of Doug Lea's LinkedBlockingQueue with Ruby related tweaks: closeable (blocks out producers, no effect on consumers) and capacity adjustable (Ruby allows sized_queue.max = 123 post construction). Relevant changes noted in comments below. An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Integer#MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.

Author
Doug Lea
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static class
Queue.Node

Linked list node class

Field Summary

Modifier and TypeField and Description
private static final RubyThread.Task<Queue, IRubyObject>
protected volatile int
capacity

The capacity bound, or Integer.MAX_VALUE if none

protected volatile boolean
protected final AtomicInteger
count

Current number of elements

pack-priv transient Queue.Node
head

Head of linked list.

protected transient Queue.Node
last

Tail of linked list.

protected final Condition
notEmpty

Wait queue for waiting takes

protected final Condition
notFull

Wait queue for waiting puts

protected final ReentrantLock
putLock

Lock held by put, offer, etc

protected final ReentrantLock
takeLock

Lock held by takeInternal, poll, etc

Inherited from org.jruby.RubyObject:
IVAR_INSPECTING_OBJECT_ALLOCATOROBJECT_ALLOCATORREIFYING_OBJECT_ALLOCATOR

Constructor Summary

AccessConstructor and Description
public
Queue(Ruby runtime, RubyClass type)

Method Summary

Modifier and TypeMethod and Description
public synchronized void
checkShutdown()
Deprecated since 10.0.

public IRubyObject
clear(ThreadContext context)

Atomically removes all of the elements from this queue.

protected void
public IRubyObject
public IRubyObject
protected void
protected RaiseException
protected IRubyObject
public RubyBoolean
protected void
public final IRubyObject
protected void
fullyLock()

Locks to prevent both puts and takes.

protected void
fullyUnlock()

Unlocks to allow both puts and takes.

public IRubyObject
public IRubyObject
protected void
initializedCheck()
Deprecated since 10.0.

protected void
public boolean
protected long
public RubyNumeric
public IRubyObject
public RubyNumeric
public IRubyObject
public IRubyObject
pollInternal(long timeout, TimeUnit unit)

public IRubyObject
pop(ThreadContext context)

public IRubyObject
pop(ThreadContext context, IRubyObject nonblockOrOpts)

public IRubyObject
pop(ThreadContext context, IRubyObject _nonblock, IRubyObject _opts)

private IRubyObject
popCommon(ThreadContext context, boolean nonblock, long timeoutNS)

public IRubyObject
push(ThreadContext context, IRubyObject value)

protected void
putInternal(ThreadContext context, IRubyObject e)

Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.

protected static long
public IRubyObject
public static RubyClass
setup(ThreadContext context, RubyClass Thread, RubyClass Object)

public static RubyClass
setupError(ThreadContext context, RubyClass Queue, RubyClass StopIteration, RubyClass Object)

public synchronized void
protected void
signalNotEmpty()

Signals a waiting takeInternal.

protected void
signalNotFull()

Signals a waiting put.

protected IRubyObject
Inherited from org.jruby.RubyObject:
attachToObjectSpacecallInitcallInitcallInitcallInitcallInitcallInitcallInitcallInitcallInitcallInitconvertToTypedigdig1dig2eqlInternalequalInternalequalsfinishObjectClassgetNativeClassIndexhashCodeinspectop_eqqputsspecificEvaltoRubyStringtoString

Field Detail

BLOCKING_TAKE_TASKback to summary
private static final RubyThread.Task<Queue, IRubyObject> BLOCKING_TAKE_TASK
capacityback to summary
protected volatile int capacity

The capacity bound, or Integer.MAX_VALUE if none

closedback to summary
protected volatile boolean closed
countback to summary
protected final AtomicInteger count

Current number of elements

lastback to summary
protected transient Queue.Node last

Tail of linked list. Invariant: last.next == null

notEmptyback to summary
protected final Condition notEmpty

Wait queue for waiting takes

notFullback to summary
protected final Condition notFull

Wait queue for waiting puts

putLockback to summary
protected final ReentrantLock putLock

Lock held by put, offer, etc

takeLockback to summary
protected final ReentrantLock takeLock

Lock held by takeInternal, poll, etc

Constructor Detail

Queueback to summary
public Queue(Ruby runtime, RubyClass type)

Method Detail

checkShutdownback to summary
public synchronized void checkShutdown()

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
clearback to summary
public IRubyObject clear(ThreadContext context)

Atomically removes all of the elements from this queue. The queue will be empty after this call returns.

Annotations
@JRubyMethod
clearInternalback to summary
protected void clearInternal() throws InterruptedException
closeback to summary
public IRubyObject close(ThreadContext context)
Annotations
@JRubyMethod
closed_pback to summary
public IRubyObject closed_p(ThreadContext context)
Annotations
@JRubyMethod
name:closed?
closeInternalback to summary
protected void closeInternal() throws InterruptedException
createInterruptedErrorback to summary
protected RaiseException createInterruptedError(ThreadContext context, String methodName)
dequeueback to summary
protected IRubyObject dequeue()
empty_pback to summary
public RubyBoolean empty_p(ThreadContext context)
Annotations
@JRubyMethod
name:empty?
enqueueback to summary
protected void enqueue(Queue.Node node)
freezeback to summary
public final IRubyObject freeze(ThreadContext context)

Overrides org.jruby.RubyBasicObject.freeze.

Annotations
@JRubyMethod
name:freeze
@Override
fullyLockback to summary
protected void fullyLock()

Locks to prevent both puts and takes.

fullyUnlockback to summary
protected void fullyUnlock()

Unlocks to allow both puts and takes.

initializeback to summary
public IRubyObject initialize(ThreadContext context)

Overrides org.jruby.RubyBasicObject.initialize.

Annotations
@JRubyMethod
visibility:PRIVATE
initializeback to summary
public IRubyObject initialize(ThreadContext context, IRubyObject items)
Annotations
@JRubyMethod
visibility:PRIVATE
initializedCheckback to summary
protected void initializedCheck()

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
initializedCheckback to summary
protected void initializedCheck(ThreadContext context)
isShutdownback to summary
public boolean isShutdown()
java_lengthback to summary
protected long java_length()
lengthback to summary
public RubyNumeric length(ThreadContext context)
Annotations
@JRubyMethod
name:length, size
marshal_dumpback to summary
public IRubyObject marshal_dump(ThreadContext context)
Annotations
@JRubyMethod
num_waitingback to summary
public RubyNumeric num_waiting(ThreadContext context)
Annotations
@JRubyMethod
pollInternalback to summary
public IRubyObject pollInternal() throws InterruptedException
pollInternalback to summary
public IRubyObject pollInternal(long timeout, TimeUnit unit) throws InterruptedException
popback to summary
public IRubyObject pop(ThreadContext context)
Annotations
@JRubyMethod
name:pop, deq, shift
popback to summary
public IRubyObject pop(ThreadContext context, IRubyObject nonblockOrOpts)
Annotations
@JRubyMethod
name:pop, deq, shift
popback to summary
public IRubyObject pop(ThreadContext context, IRubyObject _nonblock, IRubyObject _opts)
Annotations
@JRubyMethod
name:pop, deq, shift
popCommonback to summary
private IRubyObject popCommon(ThreadContext context, boolean nonblock, long timeoutNS)
pushback to summary
public IRubyObject push(ThreadContext context, IRubyObject value)
Annotations
@JRubyMethod
name:push, <<, enq
putInternalback to summary
protected void putInternal(ThreadContext context, IRubyObject e) throws InterruptedException

Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.

Exceptions
InterruptedException
NullPointerException
queueTimeoutToNanosback to summary
protected static long queueTimeoutToNanos(ThreadContext context, IRubyObject _timeout)
raiseClosedErrorback to summary
public IRubyObject raiseClosedError(ThreadContext context)
setupback to summary
public static RubyClass setup(ThreadContext context, RubyClass Thread, RubyClass Object)
setupErrorback to summary
public static RubyClass setupError(ThreadContext context, RubyClass Queue, RubyClass StopIteration, RubyClass Object)
shutdownback to summary
public synchronized void shutdown() throws InterruptedException
signalNotEmptyback to summary
protected void signalNotEmpty()

Signals a waiting takeInternal. Called only from put/offer (which do not otherwise ordinarily lock takeLock.)

signalNotFullback to summary
protected void signalNotFull()

Signals a waiting put. Called only from take/poll.

takeInternalback to summary
protected IRubyObject takeInternal(ThreadContext context) throws InterruptedException
org.jruby.ext.thread back to summary

private Class Queue.BlockingPollTask

extends Object
implements RubyThread.Task<Queue, IRubyObject>
Class Inheritance
All Implemented Interfaces
org.jruby.RubyThread.Task, org.jruby.RubyThread.Unblocker

Field Summary

Modifier and TypeField and Description
private final long

Constructor Summary

AccessConstructor and Description
public
BlockingPollTask(long timeoutNS)

Method Summary

Modifier and TypeMethod and Description
public IRubyObject
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

timeoutNSback to summary
private final long timeoutNS

Constructor Detail

BlockingPollTaskback to summary
public BlockingPollTask(long timeoutNS)

Method Detail

runback to summary
public IRubyObject run(ThreadContext context, Queue queue) throws InterruptedException

Implements org.jruby.RubyThread.Task.run.

Annotations
@Override
wakeupback to summary
public void wakeup(RubyThread thread, Queue queue)

Implements org.jruby.RubyThread.Task.wakeup.

Annotations
@Override
org.jruby.ext.thread back to summary

pack-priv Class Queue.Node

extends Object
Class Inheritance

Linked list node class

Field Summary

Modifier and TypeField and Description
pack-priv IRubyObject
pack-priv Queue.Node
next

One of: - the real successor Node - this Node, meaning the successor is head.next - null, meaning there is no successor (this is the last node)

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

itemback to summary
pack-priv IRubyObject item
nextback to summary
pack-priv Queue.Node next

One of: - the real successor Node - this Node, meaning the successor is head.next - null, meaning there is no successor (this is the last node)

Constructor Detail

Nodeback to summary
pack-priv Node(IRubyObject x)