org.jruby
Class RubyThread

java.lang.Object
  extended by org.jruby.RubyBasicObject
      extended by org.jruby.RubyObject
          extended by org.jruby.RubyThread
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<IRubyObject>, InstanceVariables, InternalVariables, IRubyObject, ExecutionContext, CoreObjectType

public class RubyThread
extends RubyObject
implements ExecutionContext

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.

See Also:
Serialized Form

Nested Class Summary
static interface RubyThread.BlockingTask
           
static class RubyThread.SleepTask
           
static class RubyThread.Status
           
 
Nested classes/interfaces inherited from class org.jruby.RubyObject
RubyObject.Data
 
Nested classes/interfaces inherited from class org.jruby.RubyBasicObject
RubyBasicObject.Finalizer
 
Field Summary
 
Fields inherited from class org.jruby.RubyObject
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, metaClass, NEVER, NIL_F, TAINTED_F, UNDEF, UNTRUSTED_F, USER0_F, USER1_F, USER2_F, USER3_F, USER4_F, USER5_F, USER6_F, USER7_F, USER8_F
 
Fields inherited from interface org.jruby.runtime.builtin.IRubyObject
NULL_ARRAY
 
Constructor Summary
protected RubyThread(Ruby runtime, RubyClass type)
           
 
Method Summary
static IRubyObject abort_on_exception_set_x(IRubyObject recv, IRubyObject value)
           
 IRubyObject abort_on_exception_set(IRubyObject val)
           
static RubyBoolean abort_on_exception_x(IRubyObject recv)
          Returns the status of the global ``abort on exception'' condition.
 RubyBoolean abort_on_exception()
           
static RubyThread adopt(IRubyObject recv, java.lang.Thread t)
           
 void afterBlockingCall()
           
 RubyBoolean alive_p()
           
 IRubyObject backtrace(ThreadContext context)
           
 void beDead()
           
 void beforeBlockingCall()
           
 void checkMail(ThreadContext context)
           
 void cleanTerminate(IRubyObject result)
           
static RubyClass createThreadClass(Ruby runtime)
           
static IRubyObject critical_set(IRubyObject receiver, IRubyObject value)
           
static IRubyObject critical(IRubyObject receiver)
           
static RubyThread current(IRubyObject recv)
           
 void dispose()
          Dispose of the current thread by removing it from its parent ThreadGroup.
 void enterSleep()
           
 boolean equals(java.lang.Object obj)
          This method is just a wrapper around the Ruby "==" method, provided so that RubyObjects can be used as keys in the Java HashMap object underlying RubyHash.
 void exceptionRaised(RaiseException exception)
           
 void executeBlockingTask(RubyThread.BlockingTask task)
           
 void exitSleep()
           
 ThreadContext getContext()
           
 java.util.Map<java.lang.Object,IRubyObject> getContextVariables()
           
 IRubyObject getErrorInfo()
           
 java.lang.Thread getNativeThread()
           
 IRubyObject group()
           
 int hashCode()
          Override the Object#hashCode method to make sure that the Ruby hash is actually used as the hashcode for Ruby objects.
 IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block)
           
 IRubyObject inspect()
          rb_obj_inspect call-seq: obj.inspect => string Returns a string containing a human-readable representation of obj.
 void internalRaise(IRubyObject[] args)
          This is intended to be used to raise exceptions in Ruby threads from non- Ruby threads like Timeout's thread.
 void interrupt()
           
 boolean isAlive()
           
 java.lang.StackTraceElement[] javaBacktrace()
           
 IRubyObject join(IRubyObject[] args)
           
 RubyBoolean key_p(IRubyObject key)
           
 RubyArray keys()
           
 IRubyObject kill_bang()
           
 IRubyObject kill()
           
static IRubyObject kill(IRubyObject receiver, IRubyObject rubyThread, Block block)
           
static RubyArray list(IRubyObject recv)
           
static RubyThread main(IRubyObject recv)
           
static RubyThread mainThread(IRubyObject receiver)
           
static IRubyObject newInstance(IRubyObject recv, IRubyObject[] args, Block block)
          Thread.new
 IRubyObject op_aref(IRubyObject key)
           
 IRubyObject op_aset(IRubyObject key, IRubyObject value)
           
static IRubyObject pass(IRubyObject recv)
           
 void pollThreadEvents()
           
 void pollThreadEvents(ThreadContext context)
           
 IRubyObject priority_set(IRubyObject priority)
           
 RubyFixnum priority()
           
 IRubyObject raise(IRubyObject[] args, Block block)
           
 void receiveMail(ThreadService.Event event)
           
 IRubyObject run()
           
static IRubyObject s_exit(IRubyObject receiver, Block block)
           
 IRubyObject safe_level()
           
 boolean select(java.nio.channels.Channel channel, RubyIO io, int ops)
           
 boolean select(java.nio.channels.Channel channel, RubyIO io, int ops, long timeout)
           
 boolean select(RubyIO io, int ops)
           
 boolean select(RubyIO io, int ops, long timeout)
           
 boolean selectForAccept(RubyIO io)
          Deprecated. 
 void setContext(ThreadContext context)
           
 IRubyObject setErrorInfo(IRubyObject errorInfo)
           
 boolean sleep(long millis)
          We can never be sure if a wait will finish because of a Java "spurious wakeup".
static RubyThread start(IRubyObject recv, IRubyObject[] args, Block block)
          Basically the same as Thread.new .
 IRubyObject status()
           
 RubyBoolean stop_p()
           
static IRubyObject stop(ThreadContext context, IRubyObject receiver)
           
 java.lang.String toString()
          The default toString method is just a wrapper that calls the Ruby "to_s" method.
 IRubyObject value()
           
 boolean wait_timeout(IRubyObject o, java.lang.Double timeout)
           
 boolean waitForIO(ThreadContext context, RubyIO io, int ops)
           
 RubyThread wakeup()
           
 
Methods inherited from class org.jruby.RubyObject
attachToObjectSpace, callInit, callInit, callInit, callInit, callInit, convertToType, createObjectClass, eqlInternal, equalInternal, getNativeTypeIndex, initialize, initialize, op_eqq, puts, specificEval
 
Methods inherited from class org.jruby.RubyBasicObject
addFinalizer, anyToString, asJavaString, asString, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callSuper, checkArrayType, checkCallMethod, checkFrozen, checkStringType, checkStringType19, compareTo, convertToArray, convertToFloat, convertToHash, convertToInteger, convertToInteger, convertToInteger, convertToString, copyInstanceVariablesInto, copySpecialInstanceVariables, createBasicObjectClass, dataGetStruct, dataGetStructChecked, dataWrapStruct, display, dup, ensureInstanceVariablesSettable, eql_p, eql, equal_p, equal_p19, evalUnder, extend, fastGetInstanceVariable, fastGetInternalVariable, fastHasInstanceVariable, fastHasInternalVariable, fastSetInstanceVariable, fastSetInternalVariable, freeze, frozen_p, getFlag, getInstanceEvalClass, getInstanceVariable, getInstanceVariableList, getInstanceVariableNameList, getInstanceVariables, getInternalVariable, getInternalVariables, getJavaClass, getMetaClass, getObjectId, getRuntime, getSingletonClass, getSingletonClassClone, getType, getVariable, getVariableCount, getVariableList, getVariableNameList, getVariableTableForRead, getVariableTableForWrite, hash, hashyInspect, hasInstanceVariable, hasInternalVariable, hasVariables, id_deprecated, id, infectBy, initialize_copy, initialize19, initialize19, initialize19, initialize19, initialize19, initObjectId, inspectHashCode, instance_eval, instance_eval, instance_eval, instance_eval, instance_eval19, instance_eval19, instance_eval19, instance_eval19, instance_exec, instance_exec19, instance_of_p, instance_variable_defined_p, instance_variable_get, instance_variable_set, instance_variables, instance_variables19, isBuiltin, isClass, isFalse, isFrozen, isImmediate, isModule, isNil, isTaint, isTrue, isUntrusted, kind_of_p, makeMetaClass, method_missing19, method, method19, methods, methods, methods19, nil_p, op_cmp, op_equal_19, op_equal, op_match, op_match19, op_not_equal, op_not_match, op_not, private_methods, private_methods19, protected_methods, protected_methods19, public_methods, public_methods19, rbClone, remove_instance_variable, removeFinalizers, removeInstanceVariable, removeInternalVariable, respond_to_p, respond_to_p, respond_to_p19, respond_to_p19, respondsTo, respondsToMissing, respondsToMissing, send, send, send, send, send, send19, send19, send19, send19, send19, setFlag, setFrozen, setInstanceVariable, setInternalVariable, setMetaClass, setTaint, setUntrusted, setVariable, singleton_method_added19, singleton_method_removed19, singleton_method_undefined19, singleton_methods, singleton_methods19, specificEval, specificEval, specificEval, specificEval, syncVariables, syncVariables, taint, taint, tainted_p, testFrozen, testFrozen, to_a, to_s, toJava, trust, type_deprecated, type, untaint, untrust, untrusted_p, 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
 

Constructor Detail

RubyThread

protected RubyThread(Ruby runtime,
                     RubyClass type)
Method Detail

receiveMail

public void receiveMail(ThreadService.Event event)

checkMail

public void checkMail(ThreadContext context)

getErrorInfo

public IRubyObject getErrorInfo()

setErrorInfo

public IRubyObject setErrorInfo(IRubyObject errorInfo)

setContext

public void setContext(ThreadContext context)

getContext

public ThreadContext getContext()

getNativeThread

public java.lang.Thread getNativeThread()

dispose

public void dispose()
Dispose of the current thread by removing it from its parent ThreadGroup.


createThreadClass

public static RubyClass createThreadClass(Ruby runtime)

newInstance

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. Any arguments passed to Thread.new are passed into the block.

 x = Thread.new { sleep .1; print "x"; print "y"; print "z" }
 a = Thread.new { print "a"; print "b"; sleep .2; print "c" }
 x.join # Let the threads finish before
 a.join # main thread exits...
 
produces: abxyzc


start

public static RubyThread start(IRubyObject recv,
                               IRubyObject[] args,
                               Block block)
Basically the same as Thread.new . However, if class Thread is subclassed, then calling start in that subclass will not invoke the subclass's initialize method.


adopt

public static RubyThread adopt(IRubyObject recv,
                               java.lang.Thread t)

initialize

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

cleanTerminate

public void cleanTerminate(IRubyObject result)

beDead

public void beDead()

pollThreadEvents

public void pollThreadEvents()

pollThreadEvents

public void pollThreadEvents(ThreadContext context)

abort_on_exception_x

public static RubyBoolean abort_on_exception_x(IRubyObject recv)
Returns the status of the global ``abort on exception'' condition. The default is false. When set to true, will cause all threads to abort (the process will exit(0)) if an exception is raised in any thread. See also Thread.abort_on_exception= .


abort_on_exception_set_x

public static IRubyObject abort_on_exception_set_x(IRubyObject recv,
                                                   IRubyObject value)

current

public static RubyThread current(IRubyObject recv)

main

public static RubyThread main(IRubyObject recv)

pass

public static IRubyObject pass(IRubyObject recv)

list

public static RubyArray list(IRubyObject recv)

getContextVariables

public final java.util.Map<java.lang.Object,IRubyObject> getContextVariables()
Specified by:
getContextVariables in interface ExecutionContext

isAlive

public boolean isAlive()

op_aref

public IRubyObject op_aref(IRubyObject key)

op_aset

public IRubyObject op_aset(IRubyObject key,
                           IRubyObject value)

abort_on_exception

public RubyBoolean abort_on_exception()

abort_on_exception_set

public IRubyObject abort_on_exception_set(IRubyObject val)

alive_p

public RubyBoolean alive_p()

join

public IRubyObject join(IRubyObject[] args)

value

public IRubyObject value()

group

public IRubyObject group()

inspect

public IRubyObject inspect()
Description copied from class: RubyBasicObject
rb_obj_inspect call-seq: obj.inspect => string Returns a string containing a human-readable representation of obj. If not overridden, uses the to_s method to generate the string. [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]" Time.new.inspect #=> "Wed Apr 09 08:54:39 CDT 2003"

Specified by:
inspect in interface IRubyObject
Overrides:
inspect in class RubyBasicObject
Returns:
String

key_p

public RubyBoolean key_p(IRubyObject key)

keys

public RubyArray keys()

critical_set

public static IRubyObject critical_set(IRubyObject receiver,
                                       IRubyObject value)

critical

public static IRubyObject critical(IRubyObject receiver)

stop

public static IRubyObject stop(ThreadContext context,
                               IRubyObject receiver)

kill

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

s_exit

public static IRubyObject s_exit(IRubyObject receiver,
                                 Block block)

stop_p

public RubyBoolean stop_p()

wakeup

public RubyThread wakeup()

priority

public RubyFixnum priority()

priority_set

public IRubyObject priority_set(IRubyObject priority)

raise

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

internalRaise

public void internalRaise(IRubyObject[] args)
This is intended to be used to raise exceptions in Ruby threads from non- Ruby threads like Timeout's thread.

Parameters:
args - Same args as for Thread#raise
block - Same as for Thread#raise

run

public IRubyObject run()

sleep

public boolean sleep(long millis)
              throws java.lang.InterruptedException
We can never be sure if a wait will finish because of a Java "spurious wakeup". So if we explicitly wakeup and we wait less than requested amount we will return false. We will return true if we sleep right amount or less than right amount via spurious wakeup.

Throws:
java.lang.InterruptedException

status

public IRubyObject status()

executeBlockingTask

public void executeBlockingTask(RubyThread.BlockingTask task)
                         throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

enterSleep

public void enterSleep()

exitSleep

public void exitSleep()

kill

public IRubyObject kill()

kill_bang

public IRubyObject kill_bang()

safe_level

public IRubyObject safe_level()

backtrace

public IRubyObject backtrace(ThreadContext context)

javaBacktrace

public java.lang.StackTraceElement[] javaBacktrace()

exceptionRaised

public void exceptionRaised(RaiseException exception)

mainThread

public static RubyThread mainThread(IRubyObject receiver)

selectForAccept

@Deprecated
public boolean selectForAccept(RubyIO io)
Deprecated. 


select

public boolean select(RubyIO io,
                      int ops)

select

public boolean select(RubyIO io,
                      int ops,
                      long timeout)

select

public boolean select(java.nio.channels.Channel channel,
                      RubyIO io,
                      int ops)

select

public boolean select(java.nio.channels.Channel channel,
                      RubyIO io,
                      int ops,
                      long timeout)

interrupt

public void interrupt()

waitForIO

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

beforeBlockingCall

public void beforeBlockingCall()

afterBlockingCall

public void afterBlockingCall()

wait_timeout

public boolean wait_timeout(IRubyObject o,
                            java.lang.Double timeout)
                     throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

equals

public boolean equals(java.lang.Object obj)
Description copied from class: RubyObject
This method is just a wrapper around the Ruby "==" method, provided so that RubyObjects can be used as keys in the Java HashMap object underlying RubyHash.

Overrides:
equals in class RubyObject

hashCode

public int hashCode()
Description copied from class: RubyObject
Override the Object#hashCode method to make sure that the Ruby hash is actually used as the hashcode for Ruby objects. If the Ruby "hash" method doesn't return a number, the Object#hashCode implementation will be used instead.

Overrides:
hashCode in class RubyObject

toString

public java.lang.String toString()
Description copied from class: RubyObject
The default toString method is just a wrapper that calls the Ruby "to_s" method.

Overrides:
toString in class RubyObject


Copyright © 2002-2009 JRuby Team. All Rights Reserved.