Package org.jruby.ext.thread
Class Queue
java.lang.Object
org.jruby.RubyBasicObject
org.jruby.RubyObject
org.jruby.ext.thread.Queue
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<IRubyObject>
,InstanceVariables
,InternalVariables
,IRubyObject
,CoreObjectType
,DataType
- Direct Known Subclasses:
SizedQueue
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.
- Since:
- 1.5
- Author:
- Doug Lea
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.jruby.RubyObject
RubyObject.Data
Nested classes/interfaces inherited from class org.jruby.RubyBasicObject
RubyBasicObject.Finalizer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
The capacity bound, or Integer.MAX_VALUE if noneprotected boolean
protected final AtomicInteger
Current number of elementsprotected org.jruby.ext.thread.Queue.Node
Tail of linked list.protected final Condition
Wait queue for waiting takesprotected final Condition
Wait queue for waiting putsprotected final ReentrantLock
Lock held by put, offer, etcprotected final ReentrantLock
Lock held by takeInternal, poll, etcFields inherited from class org.jruby.RubyObject
IVAR_INSPECTING_OBJECT_ALLOCATOR, OBJECT_ALLOCATOR, REIFYING_OBJECT_ALLOCATOR
Fields inherited from class org.jruby.RubyBasicObject
ALL_F, BASICOBJECT_ALLOCATOR, COMPARE_BY_IDENTITY_F, ERR_INSECURE_SET_INST_VAR, FALSE_F, FL_USHIFT, flags, FROZEN_F, IS_OVERLAID_F, metaClass, NEVER, NIL_F, REFINED_MODULE_F, STAMP_OFFSET, TAINTED_F, UNDEF, USER0_F, USER1_F, USER2_F, USER3_F, USER4_F, USER5_F, USER6_F, USER7_F, USER8_F, USER9_F, USERA_F, VAR_TABLE_OFFSET, varTable, varTableStamp
Fields inherited from interface org.jruby.runtime.builtin.IRubyObject
NULL_ARRAY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Deprecated.clear
(ThreadContext context) Atomically removes all of the elements from this queue.protected void
close
(ThreadContext context) closed_p
(ThreadContext context) protected void
protected RaiseException
createInterruptedError
(ThreadContext context, String methodName) protected IRubyObject
dequeue()
empty_p
(ThreadContext context) protected void
enqueue
(org.jruby.ext.thread.Queue.Node node) final IRubyObject
freeze
(ThreadContext context) protected void
Locks to prevent both puts and takes.protected void
Unlocks to allow both puts and takes.initialize
(ThreadContext context) initialize
(ThreadContext context, IRubyObject items) protected void
Deprecated.protected void
initializedCheck
(ThreadContext context) boolean
protected long
length
(ThreadContext context) marshal_dump
(ThreadContext context) num_waiting
(ThreadContext context) pollInternal
(long timeout, TimeUnit unit) pop
(ThreadContext context) pop
(ThreadContext context, IRubyObject nonblockOrOpts) pop
(ThreadContext context, IRubyObject _nonblock, IRubyObject _opts) 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
queueTimeoutToNanos
(ThreadContext context, IRubyObject _timeout) raiseClosedError
(ThreadContext context) static RubyClass
setup
(ThreadContext context, RubyClass Thread, RubyClass Object) static RubyClass
setupError
(ThreadContext context, RubyClass Queue, RubyClass StopIteration, RubyClass Object) void
shutdown()
protected void
Signals a waiting takeInternal.protected void
Signals a waiting put.protected IRubyObject
takeInternal
(ThreadContext context) Methods inherited from class org.jruby.RubyObject
attachToObjectSpace, callInit, callInit, callInit, callInit, callInit, callInit, callInit, callInit, callInit, callInit, convertToType, dig, dig1, dig2, eqlInternal, equalInternal, equals, finishObjectClass, getNativeClassIndex, hashCode, inspect, op_eqq, puts, specificEval, toRubyString, toString
Methods inherited from class org.jruby.RubyBasicObject
addFinalizer, addFinalizer, anyToString, asJavaString, asString, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callSuper, checkArrayType, checkCallMethod, checkCallMethod, checkFrozen, checkStringType, cloneSetup, compareTo, convertToArray, convertToFloat, convertToHash, convertToInteger, convertToInteger, convertToInteger, convertToString, copyInstanceVariablesInto, copySpecialInstanceVariables, dataGetStruct, dataGetStructChecked, dataWrapStruct, decode, display, dup, dupFinalizer, dupSetup, ensureInstanceVariablesSettable, eql, eql_p, eql_p, equal_p, evalUnder, extend, fastGetInstanceVariable, fastGetInternalVariable, fastHasInstanceVariable, fastHasInternalVariable, fastSetInstanceVariable, fastSetInternalVariable, finishBasicObjectClass, forEachInstanceVariable, forEachInstanceVariableName, frozen_p, getCurrentContext, getFFIHandle, getFlag, getInstanceEvalClass, getInstanceEvalClass, getInstanceVariable, getInstanceVariableList, getInstanceVariableNameList, getInstanceVariables, getInternalVariable, getInternalVariables, getJavaClass, getMarshalVariableList, getMetaClass, getMetaClass, getNativeHandle, getNativeTypeIndex, getObjectId, getRuntime, getSingletonClass, getSingletonClassClone, getSingletonClassCloneAndAttach, getSingletonClassCloneAndAttach, getType, getVariable, getVariableCount, getVariableList, getVariableNameList, hash, hash, hashyInspect, hasInstanceVariable, hasInstanceVariables, hasInternalVariable, hasVariables, id, infectBy, initialize_copy, initialize_copy, inspect, inspect, inspectHashCode, instance_eval, instance_eval, instance_eval, instance_eval, instance_eval, instance_exec, instance_of_p, instance_variable_defined_p, instance_variable_get, instance_variable_set, instance_variables, isBuiltin, isClass, isFalse, isFrozen, isImmediate, isModule, isNil, isSpecialConst, isSpecialObject, isTaint, isTrue, isUntrusted, kind_of_p, makeMetaClass, makeMetaClass, makeMetaClassBootstrap, method, method, method, method_missing, methods, methods, nil_p, nonFixnumHashCode, nonFixnumHashCode, OBJ_INIT_COPY, objInitCopy, op_cmp, op_equal, op_match, op_not, op_not_equal, op_not_match, private_methods, protected_methods, public_methods, rbClone, rbClone, rbInspect, remove_instance_variable, removeFinalizers, removeInstanceVariable, removeInternalVariable, respond_to_p, respond_to_p, respondsTo, respondsToMissing, respondsToMissing, send, send, send, send, send, setFFIHandle, setFlag, setFrozen, setInstanceVariable, setInternalVariable, setMetaClass, setNativeHandle, setTaint, setUntrusted, setVariable, singleton_method, singleton_method, singleton_method_added, singleton_method_removed, singleton_method_undefined, singleton_methods, singletonClass, specificEval, specificEval, specificEval, specificEval, syncVariables, syncVariables, taint, taint, tainted_p, testFrozen, testFrozen, to_a, to_a, to_s, to_s, toJava, trust, type, untaint, untrust, untrusted_p, validateInstanceVariable, validateInstanceVariable, validateInstanceVariable, variableTableContains, variableTableFastContains, variableTableFastFetch, variableTableFastStore, variableTableFetch, variableTableRemove, variableTableStore, variableTableSync, yieldUnder, yieldUnder
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.jruby.runtime.builtin.IRubyObject
checkStringType19, dup, marshalLiveVariables
-
Field Details
-
closed
protected volatile boolean closed -
capacity
protected volatile int capacityThe capacity bound, or Integer.MAX_VALUE if none -
count
Current number of elements -
last
protected transient org.jruby.ext.thread.Queue.Node lastTail of linked list. Invariant: last.next == null -
takeLock
Lock held by takeInternal, poll, etc -
notEmpty
Wait queue for waiting takes -
putLock
Lock held by put, offer, etc -
notFull
Wait queue for waiting puts
-
-
Constructor Details
-
Queue
-
-
Method Details
-
signalNotEmpty
protected void signalNotEmpty()Signals a waiting takeInternal. Called only from put/offer (which do not otherwise ordinarily lock takeLock.) -
signalNotFull
protected void signalNotFull()Signals a waiting put. Called only from take/poll. -
enqueue
protected void enqueue(org.jruby.ext.thread.Queue.Node node) -
dequeue
-
fullyLock
protected void fullyLock()Locks to prevent both puts and takes. -
fullyUnlock
protected void fullyUnlock()Unlocks to allow both puts and takes. -
initializedCheck
Deprecated. -
initializedCheck
-
setup
-
setupError
public static RubyClass setupError(ThreadContext context, RubyClass Queue, RubyClass StopIteration, RubyClass Object) -
initialize
- Overrides:
initialize
in classRubyBasicObject
-
initialize
-
clear
Atomically removes all of the elements from this queue. The queue will be empty after this call returns. -
clearInternal
- Throws:
InterruptedException
-
freeze
- Overrides:
freeze
in classRubyBasicObject
-
empty_p
-
length
-
num_waiting
-
pop
-
pop
-
pop
-
queueTimeoutToNanos
-
push
-
putInternal
Inserts the specified element at the tail of this queue, waiting if necessary for space to become available. -
marshal_dump
-
close
-
closeInternal
- Throws:
InterruptedException
-
closed_p
-
shutdown
- Throws:
InterruptedException
-
isShutdown
public boolean isShutdown() -
checkShutdown
Deprecated. -
java_length
protected long java_length() -
takeInternal
- Throws:
InterruptedException
-
pollInternal
- Throws:
InterruptedException
-
pollInternal
- Throws:
InterruptedException
-
raiseClosedError
-
createInterruptedError
-