Top Description Inners Fields Constructors Methods
org.jruby

public Class RubyThread

extends RubyObject
implements ExecutionContext
Class Inheritance
All Implemented Interfaces
org.jruby.runtime.ExecutionContext
Annotations
@JRubyClass
name:Thread
Imports
java.io.IOException, .Writer, java.lang.management.ManagementFactory, java.lang.ref.WeakReference, java.nio.ByteBuffer, java.nio.channels.Channel, .Channels, .SelectableChannel, .SelectionKey, .Selector, .WritableByteChannel, java.nio.charset.StandardCharsets, java.util.Iterator, .Objects, .Queue, .Vector, .WeakHashMap, .HashMap, .List, .Map, .Set, java.util.concurrent.*, java.util.concurrent.atomic.AtomicIntegerFieldUpdater, .AtomicReferenceFieldUpdater, java.util.concurrent.locks.Condition, .Lock, java.util.function.BiFunction, com.headius.backport9.stack.StackWalker, org.jcodings.Encoding, org.joni.Matcher, org.jruby.anno.JRubyClass, .JRubyMethod, org.jruby.api.Create, org.jruby.compiler.Compilable, org.jruby.exceptions.MainExitException, .RaiseException, .ThreadKill, .Unrescuable, org.jruby.internal.runtime.RubyNativeThread, .RubyRunnable, .ThreadLike, .ThreadService, .AdoptedNativeThread, org.jruby.java.proxies.ConcreteJavaProxy, org.jruby.javasupport.JavaUtil, org.jruby.runtime.Arity, .Block, .BlockBody, .ClassIndex, .Helpers, .ObjectMarshal, .ThreadContext, .ExecutionContext, org.jruby.runtime.backtrace.FrameType, .RubyStackTraceElement, org.jruby.runtime.builtin.IRubyObject, org.jruby.util.ByteList, org.jruby.util.io.BlockingIO, .ChannelFD, .OpenFile, org.jruby.util.log.Logger, .LoggerFactory, org.jruby.common.IRubyWarnings.ID

Implementation of Ruby's Thread class. Each Ruby thread is mapped to an underlying Java Virtual Machine thread.

Thread encapsulates the behavior of a thread of execution, including the main thread of the Ruby script. In the descriptions that follow, the parameter aSymbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).

Note

For CVS history, see ThreadClass.java.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static interface
public static class
public static interface
public static interface
public static class
RubyThread.SleepTask

References Deprecated RubyThread.BlockingTask is deprecated or references (maybe indirectly) at least one deprecated element.
private static class
RubyThread.SleepTask2

A Task for sleeping.

public static enum
RubyThread.Status

Thread statuses

public static interface
RubyThread.Task<Data, Return>

public static interface

Field Summary

Modifier and TypeField and Description
private volatile boolean
abortOnException

Whether this thread should try to abort the program on exception

private final boolean
adopted

Whether this is an "adopted" thread not created by Ruby code

private volatile int
private volatile BlockingIO.Condition
private volatile WeakReference<ThreadContext>
contextRef

Weak reference to the ThreadContext for this thread.

private final Map<Object, IRubyObject>
contextVariables

Context-local variables, internal-ish thread locals

private volatile RubyThread.BlockingTask
currentBlockingTask

References Deprecated RubyThread.BlockingTask is deprecated or references (maybe indirectly) at least one deprecated element.
private volatile Selector
private volatile boolean
disposed

Whether or not this thread has been disposed of

private volatile IRubyObject
errorInfo

Per-thread "current exception"

private volatile Throwable
exitingException

The exception currently being raised out of the thread.

private volatile RubyThread
private transient volatile Map<IRubyObject, IRubyObject>
fiberLocalVariables

Fiber-local variables

private String
private volatile IRubyObject
finalResult

The final value resulting from the thread's execution

private static final RubyHash.VisitorWithState<T>
private final List<Lock>
heldLocks

The list of locks this thread currently holds, so they can be released on exit

private static final AtomicIntegerFieldUpdater<RubyThread>
private static final int
private static final int
private static final int
private static final int
private volatile int
interruptFlag

Interrupt flags

private volatile int
interruptMask

Interrupt mask to use for disabling certain types

private final Vector<RubyHash>
interruptMaskStack

Stack of interrupt masks active for this thread

private volatile boolean
private int
private static final Logger
private static final RubyHash[]
private static final int
private final Queue<IRubyObject>
pendingInterruptQueue

Mail slot for cross-thread events

private volatile boolean
pendingInterruptQueueChecked

Short circuit to avoid-re-scanning for interrupts

private static final int
private volatile boolean
reportOnException

Whether this thread should report_on_exception when this thread GCs, when it terminates, or never

public static final int
public static final int
private static final String
private IRubyObject
private final RubyThread.SleepTask2
sleepTask

Thread-local tuple used for sleeping (semaphore, millis, nanos)

private volatile RubyThread.Status
status

Current status in an atomic reference

private static final AtomicReferenceFieldUpdater<RubyThread, RubyThread.Status>
private volatile RubyThreadGroup
threadGroup

The ThreadGroup to which this thread belongs

private volatile ThreadLike
threadImpl

The thread-like think that is actually executing

private transient volatile Map<IRubyObject, IRubyObject>
threadLocalVariables

Normal thread-local variables (local to parent thread if in a fiber)

private static final int
private static final int
private volatile Object
unblockArg

Argument to pass to the unblocker

private volatile RubyThread.Unblocker<Data>
unblockFunc

A function to use to unblock this thread, if possible

private static final StackWalker
Inherited from org.jruby.RubyObject:
IVAR_INSPECTING_OBJECT_ALLOCATOROBJECT_ALLOCATORREIFYING_OBJECT_ALLOCATOR

Constructor Summary

AccessConstructor and Description
protected
RubyThread(Ruby runtime, RubyClass type, boolean adopted)

public
RubyThread(Ruby runtime, RubyClass klass, Runnable runnable)

Method Summary

Modifier and TypeMethod and Description
public RubyBoolean
public static RubyBoolean
abort_on_exception(ThreadContext context, IRubyObject recv)

Returns the status of the global ``abort on exception'' condition.

public IRubyObject
public static IRubyObject
private boolean
public IRubyObject
add_trace_func(ThreadContext context, IRubyObject trace_func, Block block)

public static IRubyObject
add_trace_func(ThreadContext context, IRubyObject recv, IRubyObject trace_func, Block block)

private void
public static RubyThread
public static RubyThread
adopt(Ruby runtime, ThreadService service, Thread thread)

private static RubyThread
adoptThread(final Ruby runtime, final ThreadService service, final RubyClass recv, final Thread thread)

public void
public RubyBoolean
public RubyBoolean
alive_p()
Deprecated

private boolean
public IRubyObject
public IRubyObject
public IRubyObject
public IRubyObject
public IRubyObject
public IRubyObject
public void
public void
public void
beforeBlockingCall()
Deprecated

public void
beforeStart()

Perform pre-execution tasks once the native thread is running, but we have not yet called the Ruby code for the thread.

public void
private static int
checkInterruptMask(final ThreadContext context, final IRubyObject sym)

public synchronized void
public void
public void
clearFiberLocals()

Clear the fiber local variable storage for this thread.

public void
clearThreadLocals()

Clear the thread local variable storage for this thread.

private static void
copyInterrupts(ThreadContext context, Vector<RubyHash> sourceStack, Vector<RubyHash> targetStack)

public static RubyClass
public static RubyThread
public void
public void
dieFromFinalizer()

Used for finalizers that need to kill a Ruby thread.

public void
dispose()

Dispose of the current thread by tidying up connections to other stuff

public static IRubyObject
public void
public boolean
equals(Object
object to compare
obj
)

Overrides org.jruby.RubyObject.equals.

This override does not do a "checked" dispatch.

public void
public void
exceptionRaised(Throwable throwable)

For handling all exceptions bubbling out of threads

public void
private void
executeInterrupts(ThreadContext context, boolean blockingTiming)

public <
the type of the data object
Data
>
int

Returns:

the number of bytes written
executeReadWrite
(ThreadContext
the current context
context
,
Data
a data object
data
,
byte[]
the bytes to write
bytes
,
int
start range of bytes to write
start
,
int
length of bytes to write
length
,
RubyThread.ReadWrite<Data>
the write task
task
)

Execute an interruptible read or write operation with the given byte range and data object.

public <Data> int
executeReadWrite(ThreadContext context, Data data, ByteBuffer bytes, int start, int length, RubyThread.ReadWrite<Data> task)

public <Data> int

Returns:

""
executeRegexp
(ThreadContext context, Matcher matcher, int start, int range, int option, RubyThread.RegexMatch task)

Execute an interruptible regexp operation with the given function and bytess.

public <Data, Return> Return
executeTask(ThreadContext context, Data data, RubyThread.Task<Data, Return> task)

public <Data, Return> Return
executeTask(ThreadContext context, Data data, RubyThread.Status status, RubyThread.Task<Data, Return> task)

private <Data, Return> Return
executeTask(ThreadContext context, Data data, RubyThread.Status status, RubyThread.Task<Data, Return> task, boolean blocking)

public <Data, Return> Return
executeTaskBlocking(ThreadContext context, Data data, RubyThread.Task<Data, Return> task)

public <Data, Return> Return
executeTaskBlocking(ThreadContext context, Data data, RubyThread.Status status, RubyThread.Task<Data, Return> task)

public static IRubyObject
exit(IRubyObject receiver, Block block)

public void
public IRubyObject
fetch(ThreadContext context, IRubyObject key, Block block)

public IRubyObject
fetch(ThreadContext context, IRubyObject key, IRubyObject _default, Block block)

private IRubyObject
genericKill(Ruby runtime, RubyThread currentThread)

private IRubyObject
genericRaise(ThreadContext context, RubyThread currentThread, IRubyObject... args)

public ThreadContext
public final Map<Object, IRubyObject>
public IRubyObject
public Throwable

Returns:

existing exception or null if terminated normally
getExitingException
()

public RubyThread
private Map<IRubyObject, IRubyObject>
private int
public IRubyObject
getName()
Deprecated since 10.0.

public IRubyObject
public Thread
public IRubyObject
public IRubyObject
private RubyThread.Status
private String
public RubyThreadGroup
private Map<IRubyObject, IRubyObject>
private Map<IRubyObject, IRubyObject>
public IRubyObject
public static IRubyObject
private IRubyObject
private <StateType> IRubyObject
handleInterrupt(ThreadContext context, RubyHash mask, StateType state, BiFunction<ThreadContext, StateType, IRubyObject> block)

public int
hashCode()

Overrides org.jruby.RubyObject.hashCode.

This override does not do "checked" dispatch since Object usually has #hash defined.

private String
private static long
incAndGetThreadCount(final Ruby runtime)

public void
public IRubyObject
initialize(ThreadContext context, IRubyObject[] args, Block block)

private static void
initThreadName(final Ruby runtime, final Thread thread, final String file, final int line)

public RubyString
inspect(ThreadContext context)

Overrides org.jruby.RubyBasicObject.inspect.

Overrides default org.jruby.runtime.builtin.IRubyObject.inspect.

Returns a string containing a human-readable representation of obj.

public synchronized void
public boolean
public boolean
public boolean
public boolean
private boolean
public boolean
public StackTraceElement[]
public static int
javaPriorityToRubyPriority(int javaPriority)

public IRubyObject
public IRubyObject
join(ThreadContext context, IRubyObject timeout)

public IRubyObject
join(ThreadContext context, IRubyObject[] args)
Deprecated

private IRubyObject
joinCommon(ThreadContext context, long timeoutMillis)

public RubyBoolean
public RubyArray<T extends IRubyObject>
keys()

public static IRubyObject
kill(IRubyObject recv, IRubyObject rubyThread, Block block)

public IRubyObject
kill()

public static RubyArray<T extends IRubyObject>
public void
lock(Lock
the lock to acquire, released on thread termination
lock
)

Acquire the given lock, holding a reference to it for cleanup on thread termination.

public void
lockInterruptibly(Lock
the lock to acquire, released on thread termination
lock
)

Acquire the given lock interruptibly, holding a reference to it for cleanup on thread termination.

public static RubyThread
public static RubyThread
public IRubyObject
private RubyThread.Status
public static IRubyObject
newInstance(IRubyObject recv, IRubyObject[] args, Block block)

Thread.new

Thread.new( [ arg ]* ) {| args | block }$ -> aThread

Creates a new thread to execute the instructions given in block, and begins running it.

private static boolean
public IRubyObject
public IRubyObject
public IRubyObject
op_aset(IRubyObject key, IRubyObject value)
Deprecated

public static IRubyObject
public IRubyObject
public IRubyObject
public IRubyObject
pending_interrupt_p(ThreadContext context, IRubyObject[] args)
Deprecated

public static IRubyObject
pending_interrupt_p(ThreadContext context, IRubyObject self, IRubyObject[] args)
Deprecated

public static IRubyObject
public static IRubyObject
private boolean
private int
private void
private IRubyObject
pendingInterruptDeque(ThreadContext context, int timing)

private void
private boolean
public void
public void
pollThreadEvents(ThreadContext
the context
context
,
boolean
whether to trigger blocking operation interrupts
blocking
)

Poll for thread events (raise, kill), propagating only events that are unmasked (see handle_interrupt(ThreadContext, IRubyObject, IRubyObject, Block) or intended to fire before blocking operations if blocking = true.

public void
private void
private void
public static IRubyObject
private <Data> void
preReadWrite(ThreadContext context, Data data, RubyThread.ReadWrite<Data> task)

protected void
public RubyFixnum
priority()
Deprecated since 10.0.

public RubyFixnum
public IRubyObject
priority_set(IRubyObject priority)
Deprecated since 10.0.

public IRubyObject
public final IRubyObject

Returns:

this thread
raise
(IRubyObject
the exception to raise
exception
)

Simplified utility method for just raising an existing exception in this thread.

public final IRubyObject

Returns:

this thread
raise
(IRubyObject
the exception to raise
exception
,
RubyString
the message to use
message
)

Simplified utility method for just raising an existing exception in this thread.

public IRubyObject
raise(ThreadContext context, IRubyObject[] args, Block block)

public IRubyObject
public static IRubyObject
public IRubyObject
public static IRubyObject
public static int
rubyPriorityToJavaPriority(int rubyPriority)

public synchronized IRubyObject
run()

public boolean

Returns:

true if the IO's channel became ready for the requested operations, false if it was not selectable.
select
(RubyIO
the RubyIO that contains the channel, for managing blocked threads list.
io
,
int
the operations to wait for, from java.nio.channels.SelectionKey.
ops
)

Perform an interruptible select operation on the given channel and fptr, waiting for the requested operations or the given timeout.

public boolean

Returns:

true if the IO's channel became ready for the requested operations, false if it timed out or was not selectable.
select
(RubyIO
the RubyIO that contains the channel, for managing blocked threads list.
io
,
int
the operations to wait for, from java.nio.channels.SelectionKey.
ops
,
long
a timeout in ms to limit the select. Less than zero selects forever, zero selects and returns ready channels or nothing immediately, and greater than zero selects for at most that many ms.
timeout
)

Perform an interruptible select operation on the given channel and fptr, waiting for the requested operations or the given timeout.

public boolean

Returns:

true if the channel became ready for the requested operations, false if it was not selectable.
select
(Channel
the channel to perform a select against. If this is not a selectable channel, then this method will just return true.
channel
,
OpenFile
the fptr that contains the channel, for managing blocked threads list.
fptr
,
int
the operations to wait for, from java.nio.channels.SelectionKey.
ops
)

Perform an interruptible select operation on the given channel and fptr, waiting for the requested operations.

public boolean

Returns:

true if the channel became ready for the requested operations, false if it was not selectable.
select
(Channel
the channel to perform a select against. If this is not a selectable channel, then this method will just return true.
channel
,
RubyIO
the RubyIO that contains the channel, for managing blocked threads list.
io
,
int
the operations to wait for, from java.nio.channels.SelectionKey.
ops
)

Perform an interruptible select operation on the given channel and fptr, waiting for the requested operations.

public boolean

Returns:

true if the channel became ready for the requested operations, false if it timed out or was not selectable.
select
(Channel
the channel to perform a select against. If this is not a selectable channel, then this method will just return true.
channel
,
RubyIO
the RubyIO that contains the channel, for managing blocked threads list.
io
,
int
the operations to wait for, from java.nio.channels.SelectionKey.
ops
,
long
a timeout in ms to limit the select. Less than zero selects forever, zero selects and returns ready channels or nothing immediately, and greater than zero selects for at most that many ms.
timeout
)

Perform an interruptible select operation on the given channel and fptr, waiting for the requested operations or the given timeout.

public boolean

Returns:

true if the channel became ready for the requested operations, false if it timed out or was not selectable.
select
(Channel
the channel to perform a select against. If this is not a selectable channel, then this method will just return true.
channel
,
OpenFile
the fptr that contains the channel, for managing blocked threads list.
fptr
,
int
the operations to wait for, from java.nio.channels.SelectionKey.
ops
,
long
a timeout in ms to limit the select. Less than zero selects forever, zero selects and returns ready channels or nothing immediately, and greater than zero selects for at most that many ms.
timeout
)

Perform an interruptible select operation on the given channel and fptr, waiting for the requested operations or the given timeout.

public IRubyObject
set_trace_func(ThreadContext context, IRubyObject trace_func, Block block)

public void
setAbortOnException(final boolean abortOnException)

public void
setContext(final ThreadContext context)

public IRubyObject
public void
setFiberCurrentThread(RubyThread fiberCurrentThread)

public IRubyObject
setFiberScheduler(IRubyObject scheduler)
Deprecated since 10.0.

public IRubyObject
setFiberScheduler(ThreadContext context, IRubyObject scheduler)

Set the scheduler for the current thread.

public void
setInterrupt()

Set the pending interrupt flag.

public IRubyObject
setName(IRubyObject name)
Deprecated since 10.0.

public IRubyObject
public void
setReportOnException(final boolean reportOnException)

pack-priv void
public boolean
sleep(long
Number of milliseconds to sleep. Zero sleeps forever.
milliseconds
)

Sleep the current thread for milliseconds, waking up on any thread interrupts.

public boolean
sleep(long
Number of milliseconds to sleep. Combined with nanoseconds, zero sleeps forever.
milliseconds
,
long
Number of nanoseconds to sleep. Combined with milliseconds, zero sleeps forever.
nanoseconds
)

Sleep the current thread for milliseconds + nanoseconds, waking up on any thread interrupts.

public void
sleep(Lock lock)

Release lock and sleep.

public void
sleep(Lock lock, long millis)

Release lock and sleep for the specified number of milliseconds.

public static RubyThread
start(IRubyObject recv, IRubyObject[] args, Block block)
Deprecated since 10.0.

public static RubyThread
start(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block)

Basically the same as Thread.new .

private Thread
startThread(ThreadContext context, Runnable runnable, String file, int line)

private static RubyThread
startThread(final IRubyObject recv, final IRubyObject[] args, boolean callInit, Block block)

protected static RubyThread
startWaiterThread(final Ruby runtime, long pid, Block block)

public IRubyObject
public IRubyObject
public static IRubyObject
stop(ThreadContext context, IRubyObject receiver)

public RubyBoolean
public RubyBoolean
stop_p()
Deprecated

public IRubyObject
public IRubyObject
public IRubyObject
public IRubyObject
private void
throwThreadKill()

MRI: rb_threadptr_to_kill

public <T> T
toJava(final Class<T>
The target type to which the object should be converted (e.g. java.lang.Thread.class).
target
)

Overrides org.jruby.RubyBasicObject.toJava.

Implements org.jruby.runtime.builtin.IRubyObject.toJava.

Customized for retrieving a Java thread from a Ruby one.

private void
public String
toString()

Overrides org.jruby.RubyObject.toString.

The default toString method is just a wrapper that calls the Ruby "to_s" method.

public boolean
tryLock(Lock
the lock to acquire, released on thread termination
lock
)

Try to acquire the given lock, adding it to a list of held locks for cleanup on thread termination if it is acquired.

public static <StateType> IRubyObject

Returns:

return value of f.apply.
uninterruptible
(ThreadContext
the current context
context
,
StateType state, BiFunction<ThreadContext, StateType, IRubyObject>
the bifunction to execute
f
)

Run the provided BiFunction without allowing for any cross-thread interrupts (equivalent to calling handle_interrupt(ThreadContext, IRubyObject, IRubyObject, Block) with Object => :never.

public void
unlock(Lock
the lock to release and dereferences
lock
)

Release the given lock and remove it from the list of locks to be released on thread termination.

public void
unlockAll()

Release all locks held.

public IRubyObject
public IRubyObject
value()
Deprecated

public boolean
public boolean
waitForIO(ThreadContext context, RubyIO io, int ops)

public synchronized RubyThread
Inherited from org.jruby.RubyObject:
attachToObjectSpacecallInitcallInitcallInitcallInitcallInitcallInitcallInitcallInitcallInitcallInitconvertToTypedigdig1dig2eqlInternalequalInternalfinishObjectClassgetNativeClassIndexinspectop_eqqputsspecificEvaltoRubyString