Thread encapsulates the behavior of a thread of execution, including the main thread of the Ruby script. See the tutorial on “Threads and Processes”. In the descriptions that follow, the parameter aSymbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).
true or falsefalse. 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=. true or falsetrue, all threads will abort if an exception is raised. Returns the new state.
Thread.abort_on_exception = true
t1 = Thread.new do
puts "In second thread"
raise "Raise exception"
end
t1.join
print "not reached\n"produces:
In second thread
prog.rb:4: Raise exception (RuntimeError)
from prog.rb:2:in `initialize'
from prog.rb:2:in `new'
from prog.rb:2true or falsetrue or falsetrue, prohibits scheduling of any existing thread. Does not block new threads from being created and run. Certain thread operations (such as stopping or killing a thread, sleeping in the current thread, and raising an exception) may cause a thread to be scheduled even when in a critical section.
count=0
Thread.new { while true; sleep(1); print "a "; count+=1; end }
while count < 3 do end # no-op wait
Thread.critical = true
puts "no more a's will come out."produces:
a a a no more a's will come out.Thread.current → #<Thread:0x401be5c8 run>exit returns the Thread. If this is the main thread, or the last thread, exit the process. Thread.new.
Thread.exit).
count = 0
a = Thread.new { while true do count += 1 end }
sleep(1) → 1
Thread.kill(a) → #<Thread:0x401b5cac dead>
count → 903877
a.alive? → falseThread objects for all threads that are either runnable or stopped.
Thread.new { sleep(200) }
Thread.new { 1000000.times {|i| i*i } }
Thread.new { Thread.stop }
l = Thread.list
l → [#<Thread:0x401b5644 sleep>, #<Thread:0x401b59f0 run>, #<Thread:0x401b5cac sleep>, #<Thread:0x401be5c8 run>]Thread.main → #<Thread:0x401be5c8 run>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:
abxyzca = Thread.new { print "a"; Thread.pass;
print "b"; Thread.pass;
print "c" }
b = Thread.new { print "x"; Thread.pass;
print "y"; Thread.pass;
print "z" }
a.join
b.joinproduces:
axbyczThread.new. However, if class Thread is subclassed, then calling start in that subclass will not invoke the subclass's initialize method. false.
a = Thread.new { print "a"; Thread.stop; print "c" }
Thread.pass
print "b"
a.run
a.joinproduces:
abcnilnil.
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>nilThread#[]. true or falsefalse. See also Thread.abort_on_exception=. true or false → true or falsetrue, causes all threads (including the main program) to abort if an exception is raised in thr. The process will effectively exit(0). true or falsetrue if thr is running or sleeping. Thread.current.alive? → truenilexit returns the Thread. If this is the main thread, or the last thread, exits the process. 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:
axyztrue or falsetrue 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) → falseThread#exit. Thread.current.priority → 0count1 = count2 = 0
a = Thread.new do
loop { count1 += 1 }
end
a.priority = -1b = Thread.new do
loop { count2 += 1 }
end
b.priority = -2
sleep 1 → 1
Thread.critical = 1
count1 → 577581
count2 → 5751Kernel::raise 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:2a = Thread.new { puts "a"; Thread.stop; puts "c" }
Thread.pass
puts "Got here"
a.run
a.joinproduces:
a
Got here
cThread.current.safe_level → 0false or nilsleep” 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"true or falsetrue if thr is dead or sleeping.
a = Thread.new { Thread.stop }
b = Thread.current
a.stop? → true
b.stop? → falseThread#join) and returns its value.
a = Thread.new { 2+2 }
a.value → 4Thread#run).
c = Thread.new { Thread.stop; puts "hey!" }
c.wakeupproduces:
hey!Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.