Skip to content
Related Articles

Related Articles

Ruby | Thread Life Cycle & Its States
  • Last Updated : 19 Dec, 2018

The Thread life cycle provides the brief description of the thread from its birth to its end. A new thread can be created with the help of or Thread.start, or Thread.fork methods. There is no need to start a new thread after creating. A thread starts running automatically when the resources of the CPU is available. The value of the a citation is a Thread object. The Thread Class provides a variety of methods to query and manipulate the thread.
A thread runs into the block of code that is related to the call to and then stops running. The value of the last expression in the thread block is the value of the thread and the value is acquired by calling the value method of the Thread object. The value method return value only if the thread has run completely otherwise it does not return value to the thread that runs completely. If an exception raised in the thread, then it terminates the running thread. This condition works only on those threads which are not main thread and only those threads terminate which contains an exception.

Thread States

In Ruby, there are five states available for threads which show the status of the thread. You can check the status of a thread by using alive? and status methods.

  1. Runnable: The thread which is currently running or that is ready to take CPU resources when they are available.
  2. Sleeping: The thread which is currently sleeping, or that is waiting for IO, or that is stopped itself.
  3. Aborting: It is a intermediate state. An aborting thread in which it has been killed, but that has not yet terminated.
  4. Terminated with exception: A thread contains exception, or in other words a thread which is terminated due to the rise of exception.
  5. Terminated normally: The thread which terminates normally, or that does not contain an exception and completed its work.


    # Ruby program to illustrate 
    # check status of thread
    counter = 0
    # creating new thread
    x = { loop { counter += 1 } }
    # check thread alive or not
    puts x.alive? 



The following table shows the name of states and their return values:

StatesReturn Value
Terminated normallyfalse
Terminated with exceptionnil

Main Thread

In Ruby, the main thread is a special part of multi-threading. It is the topmost thread of a program all the subthreads are run under this thread or in other words, it is the parent thread and other thread of the same program are the child thread of the main thread. The main thread is created with the help of main method. The Ruby interpreter stop running when the work of the main thread is complete means main thread and child thread complete their tasks. The class method Thread.main returns the Thread object that represents the main thread. If an exception raised in the main thread and not handled in anywhere then, the Ruby interpreter print a message or exit. And if an exception raised in the thread other than main thread, then Ruby interpreter terminate the thread that contains an exception


# Ruby program to illustrate 
# main thread
# Create main thread
puts Thread.main  
puts ""  
# create new thread 
a1 = {sleep 200}  
list_thread= Thread.list
list_thread.each {|t| p t }  
puts "Current thread = " + Thread.current.to_s  
 # create new thread
a2 = {sleep 200}  
list_thread= Thread.list
list_thread.each {|t| p t }  
puts "Current thread=" + Thread.current.to_s   
 # kill thread a1
# pass execution to thread a2 
# kill thread a2                         
list_thread= Thread.list
list_thread.each {|t| p t }  
# exit main thread



#<Thread:0x00000001afe1b8 run>
#<Thread:0x00000001d05c68@/var/www/service/usercode/1749234900/source.rb:9 sleep>
Current thread = #<Thread:0x00000001afe1b8>
#<Thread:0x00000001afe1b8 run>
#<Thread:0x00000001d05c68@/var/www/service/usercode/1749234900/source.rb:9 sleep>
#<Thread:0x00000001c8bd50@/var/www/service/usercode/1749234900/source.rb:15 run>
Current thread=#<Thread:0x00000001afe1b8>
#<Thread:0x00000001afe1b8 run>
#<Thread:0x00000001c8bd50@/var/www/service/usercode/1749234900/source.rb:15 dead>

Explanation: This program shows that how the main thread executes. First of all, we create the main thread and then in this main thread there are two sub-thread, i.e. a1 and a2 thread. When thread a1 executes completely, then kill the a1 thread and pass the execution to the a2 thread. After that kill a2 thread and print the list of threads present in the main thread with their status. When all the threads present in the main thread are dead, then the main thread exists.

Alternate Thread States: Pausing, Waking, and Killing

As we know that threads are created in the runnable state and ready to run. A thread pauses itself by entering into a sleeping state, or by calling Thread.stop method, or by calling Kernel.sleep. No thread can forcefully pause by another thread. If a thread call Kernel.sleep with no argument, then it pauses the thread forever or until woken up and if a thread call Kernel.sleep with an argument, then it temporarily put the thread into sleeping state. The thread which is in a temporary sleeping state wakes up automatically when the given time expired and re-entered into the runnable state.

A thread pause by calling Thread.stop or by calling kernel.sleep can start again by calling instance methods, i.e. wake up and run. These methods switch the states of a thread from sleeping to runnable. The run method also calls a thread scheduler. Due to the calling of thread scheduler, the recently awaken thread may get CPU resources. The wakeup method wakes up the specific thread without calling thread scheduler.

A thread can forcefully terminate other thread by calling instance method kill. Terminate and exit method is similar to kill method. These methods place the killed method into the terminate normally state. Killing a thread is hazardous things to do unless you have a way of knowing that the thread is not in the middle of the file sharing state. Killing a thread with ! method is more harmful because a killed thread may leave the sockets, files, and other resources open.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :