Thread Status

Each thread has a status that may be one of the following:

run

When the thread is executing

sleep

When the thread is sleeping or waiting on I/O

aborting

When the thread is aborting

false

When the thread terminated normally

nil

When the thread terminated with an exception

You can obtain the status of a thread using the status method. The status is also shown when you inspect a thread, in which case either a nil or a false status is shown as dead.

thread_status.rb

puts( Thread.main.inspect )               #=> #<Thread:0x28955c8 run>
puts( Thread.new{ sleep }.kill.inspect )  #=> #<Thread:0x28cddc0 dead>
puts( Thread.new{ sleep }.inspect )       #=> #<Thread:0x28cdd48 sleep>
thread1 = Thread.new{ }
puts( thread1.status )                    #=> false
thread2 = Thread.new{ raise( "Exception raised!" ) }
puts( thread2 )                           #=> nil

Note that the status shown may differ according to the version of Ruby being used and also when the program is run at different times. This is because actions on threads may not occur instantly, and the timing of a change in status may vary with each execution. For example, sometimes you may see the status of a killed thread shown as “aborting” and at other times as “dead.” The thread aborts before it dies, and its change in status may happen in milliseconds. Here is an example taken from the Ruby class library documentation. The documented status of each thread is shown in the comments:

thread_status2.rb

p   d.kill                  #=> #<Thread:0x401b3678 aborting>
p   a.status                #=> nil
p   b.status                #=> "sleep"
p   c.status                #=> false
p   d.status                #=> "aborting"
p   Thread.current.status   #=> "run"

But when I run this code with Ruby 1.9, the status varies greatly, and it does not always match the status shown in the documented example shown earlier. At one moment, this is what I see:

#<Thread:0x401b3678 aborting>
"run"
"sleep"
false
false
"run"

But when I run it again, this is what I see:

#<Thread:0x401b3678 aborting>
"run"
"run"
"run"
false
"run"

Now look at this program:

thread_status3.rb

t =  Thread.new{  }
p t
p t.kill
# sleep( 1 )      # try uncommenting this
puts( t.inspect )

Once again the output varies each time it is run. I often see the following, which shows that even after I have “killed” the thread, it may still be “aborting” when I test its status:

#<Thread:0x2be6420 run>
#<Thread:0x2be6420 aborting>
#<Thread:0x2be6420 aborting>

Now I force a time delay by calling sleep for one second:

sleep( 1 )
puts( t.inspect )

This time the thread has time to be terminated, and this is displayed:

#<Thread:0x2be6420 dead>

These timing issues are more likely to arise in Ruby 1.9 than in older versions. You need to be aware of them and, if necessary, check a thread’s status repeatedly in order to verify that it is in the state expected at any given moment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.119.106