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. The value of the Thread.new 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 Thread.new 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.
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.
- Runnable: The thread which is currently running or that is ready to take CPU resources when they are available.
- Sleeping: The thread which is currently sleeping, or that is waiting for IO, or that is stopped itself.
- Aborting: It is a intermediate state. An aborting thread in which it has been killed, but that has not yet terminated.
- Terminated with exception: A thread contains exception, or in other words a thread which is terminated due to the rise of exception.
- Terminated normally: The thread which terminates normally, or that does not contain an exception and completed its work.
The following table shows the name of states and their return values:
|Terminated with exception||nil|
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
Current thread = #<Thread:0x00000001afe1b8>
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.