#Thread #1 is running Herethread. New { #Thread #This code} #Thread # This code
Thread.new is synonymous with Thread.Start and thread.for.
The value of the last expression in the code block is the value of the thread, which can be obtained by invoking the value of the thread object.
The thread.current represents the thread object for the current threads.
Thread.main is the main thread that represents the beginning of the Ruby program
Thread.Join blocking the current thread knows that the join thread has finished executing
Thread.abort_on_exception = True if there are unhandled exceptions exit the interpreter
Threads can store data that is part of a thread
thread.current['count'] = 0puts thread.current['count' ]
Thread Mutex
#!/usr/bin/rubyRequire'Thread'Mutex=Mutex.newcount1= Count2 =0difference=0counter=thread.new Do loop does mutex.synchronize do count1+ = 1Count2+ = 1End Endendspy=thread.new Do loop does mutex.synchronize do difference+ = (Count1-count2). ABS End Endendsleep1mutex.lockputs"count1: #{count1}"puts"count2: #{count2}"puts"difference: #{difference}"
Thread state
Thread State |
Return value |
Runnable |
Run |
Sleeping |
Sleeping |
Aborting |
Aborting |
Terminated normally |
False |
Terminated Width exception |
Nil |
# !/usr/bin/ruby = thread.new do # calling a class method new puts "in second Thread" raise
"
Raise exception
"
endthr.join
#
Ruby's multi-threading