Ruby | Thread Life Cycle & Its States (original) (raw)

Last Updated : 27 Sep, 2025

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 Thread.new 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.

**Note: 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.

**Example:

Ruby `

counter = 0 x = Thread.new { loop { counter += 1 } } puts x.alive?

`

**Output:

true

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

States Return Value
Runnable run
Sleeping sleep
Aborting aborting
Terminated normally false
Terminated with exception nil

Main Thread

In Ruby, the main thread is a special part of multi-threading. It is the topmost thread of a program all the sub-threads 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.

**Example:

Ruby `

Ruby program to illustrate

main thread

Create main thread

puts Thread.main
puts ""

create new thread

a1 = Thread.new {sleep 200}
list_thread= Thread.list list_thread.each {|t| p t }
puts "Current thread = " + Thread.current.to_s

create new thread

a2 = Thread.new {sleep 200}
list_thread= Thread.list list_thread.each {|t| p t }
puts "Current thread=" + Thread.current.to_s

kill thread a1

Thread.kill(a1)

pass execution to thread a2

Thread.pass

kill thread a2

Thread.kill(a2)

list_thread= Thread.list list_thread.each {|t| p t }

exit main thread

Thread.exit

`

**Output:

#Thread:0x00000001afe1b8 #<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:

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.

**Note: Killing a thread with ! method is more harmful because a killed thread may leave the sockets, files, and other resources open.