|
instance methods
|
|
[ ]
|
thr[ aSymbol ] -> anObject or nil
|
|
Attribute Reference---Returns the value of a thread-local
variable, using either a symbol or a string name. If the
specified variable does not exist, returns nil.
a = Thread.new { Thread.current["name"] = "A"; Thread.stop }
b = Thread.new { Thread.current[:name] = "B"; Thread.stop }
c = Thread.new { Thread.current["name"] = "C"; Thread.stop }
Thread.list.each {|x| print x.inspect, x[:name], "\n" }
|
produces:
#<Thread:0x401b53c4 sleep>C
#<Thread:0x401b5734 sleep>B
#<Thread:0x401b5cac sleep>A
#<Thread:0x401be5c8 run>nil
|
|
|
[ ]=
|
thr[ aSymbol ] = anObject-> anObject
|
|
Attribute Assignment---Sets or creates the value of a thread-local
variable, using either a symbol or a string. See also
Thread#[]
.
|
|
abort_on_exception
|
thr.abort_on_exception
-> true or false
|
|
Returns the status of the ``abort on exception''
condition for thr. The default is false.
See also
Thread.abort_on_exception=
.
|
|
abort_on_exception=
|
thr.abort_on_exception=
true or false-> true or false
|
|
When set to true, causes all threads (including the main
program) to abort if an exception is raised in thr.
The process will effectively exit(0).
|
|
alive?
|
thr.alive?
-> true or false
|
|
Returns true if thr is running or sleeping.
Thread.current.alive?
|
� |
true
|
|
|
exit
|
thr.exit -> thr or nil
|
|
Terminates thr and schedules another
thread to be run. If this thread is already marked to be
killed, exit returns the Thread. If this is the main thread, or
the last thread, exits the process.
|
|
join
|
thr.join
-> thr
|
|
The calling thread will suspend execution and run thr. Does
not return until
thr exits. Any threads not joined will be killed when the
main program exits.
a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
x.join # Let x thread finish, a will be killed on exit.
|
produces:
|
|
key?
|
thr.key?( aSymbol ) -> true or false
|
|
Returns true if the given string (or symbol) exists as a
thread-local variable.
me = Thread.current
|
me[:oliver] = "a"
|
me.key?(:oliver)
|
� |
true
|
me.key?(:stanley)
|
� |
false
|
|
|
kill
|
thr.kill
|
|
Synonym for
Thread#exit
.
|
|
priority
|
thr.priority
-> anInteger
|
|
Returns the priority of thr. Default is zero;
higher-priority threads will run before lower-priority threads.
Thread.current.priority
|
� |
0
|
|
|
priority=
|
thr.priority= anInteger
-> thr
|
|
Sets the priority of thr to anInteger.
Higher-priority threads will run before lower-priority threads.
count1 = count2 = 0
a = Thread.new do
loop { count1 += 1 }
end
a.priority = -1
|
b = Thread.new do
|
loop { count2 += 1 }
|
end
|
b.priority = -2
|
sleep 1
|
� |
1
|
Thread.critical = 1
|
count1
|
� |
577581
|
count2
|
� |
5751
|
|
|
raise
|
thr.raise( anException )
|
|
Raises an exception (see
Kernel::raise
on page 420 for details) from thr.
The caller does not have to be thr.
Thread.abort_on_exception = true
a = Thread.new { sleep(200) }
a.raise("Gotcha")
|
produces:
prog.rb:3: Gotcha (RuntimeError)
from prog.rb:2:in `initialize'
from prog.rb:2:in `new'
from prog.rb:2
|
|
|
run
|
thr.run -> thr
|
|
Wakes up thr, making it eligible for scheduling. If not
in a critical section, then invokes the scheduler.
a = Thread.new { puts "a"; Thread.stop; puts "c" }
Thread.pass
puts "Got here"
a.run
a.join
|
produces:
|
|
safe_level
|
thr.safe_level
-> anInteger
|
|
Returns the safe level in effect for thr.
Thread.current.safe_level
|
� |
0
|
|
|
status
|
thr.status
-> aString, false or nil
|
|
Returns the status of thr: ``sleep'' if thr is sleeping
or waiting on I/O, ``run'' if thr is executing,
false if thr terminated normally, and nil if
thr terminated with an exception.
a = Thread.new { raise("die now") }
|
b = Thread.new { Thread.stop }
|
c = Thread.new { Thread.exit }
|
a.status
|
� |
nil
|
b.status
|
� |
"sleep"
|
c.status
|
� |
false
|
Thread.current.status
|
� |
"run"
|
|
|
stop?
|
thr.stop?
-> true or false
|
|
Returns true if thr is dead or sleeping.
a = Thread.new { Thread.stop }
|
b = Thread.current
|
a.stop?
|
� |
true
|
b.stop?
|
� |
false
|
|
|
value
|
thr.value
-> anObject
|
|
Waits for thr to complete (via
Thread#join
) and
returns its value.
a = Thread.new { 2+2 }
|
a.value
|
� |
4
|
|
|
wakeup
|
thr.wakeup
-> thr
|
|
Marks thr as eligible for scheduling (it may still remain
blocked on I/O, however). Does not invoke the scheduler (see
Thread#run
).
c = Thread.new { Thread.stop; puts "hey!" }
c.wakeup
|
produces:
|