In this article,Life cycle of a Thread is explained in detail with examples.
When you are programming with threads, understanding the life cycle of a thread is very valuable. While a thread is alive, it is in one of the several states.
A thread can be in one of the five states in the thread. According to the sun, there are only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads/Life cycle of a Thread, we are explaining it in the 5 states. The life cycle of a thread is controlled by JVM. The thread states are as follows:
- New.
- Runnable(Ready).
- Running.
- Non-Runnable (Blocked, Sleeping, Waiting).
- Terminated(Dead).
States of the thread- A thread has one of the following States.(From Topic Life cycle of a Thread)
New-A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
1-Runnable- After invocation of start() method on a new thread, the thread becomes runnable.After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
2-Running-A thread is in running state that means the thread is currently executing. There are several ways to enter in the Runnable state but there is only one way to enter in Running state: the scheduler selects a thread from the runnable pool.
3-Blocked- This is the state when a thread is waiting for a lock to access an object.
4-Waiting-Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
5-Timed waiting- A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
6-Not Runnable- after Runnable states these three states are assumed to be in a not Runnable state. These three states are waiting, Timed_waiting (sleeping) and Terminated.
7-Terminated- In this state the thread is dead.
New-A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
1-Runnable- After invocation of start() method on a new thread, the thread becomes runnable.After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
2-Running-A thread is in running state that means the thread is currently executing. There are several ways to enter in the Runnable state but there is only one way to enter in Running state: the scheduler selects a thread from the runnable pool.
3-Blocked- This is the state when a thread is waiting for a lock to access an object.
4-Waiting-Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
5-Timed waiting- A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
6-Not Runnable- after Runnable states these three states are assumed to be in a not Runnable state. These three states are waiting, Timed_waiting (sleeping) and Terminated.
7-Terminated- In this state the thread is dead.
We use setName(String) and getName() methods to work with names of a thread.
However,we can also set the name of a thread while creating the object of thread class,by passing string name as a parameter to the constructor of that class and in the constructor definition, we should call base class Thread constructor with string as a parameter.Hence, string name becomes your thread name.
Ex
//program related to Life cycle of a Thread.
//program related to Life cycle of a Thread.
class SimpleThread extends Thread
{
public SimpleThread() //default constructor.
{
}
public SimpleThread(String str) //parametric constructor.
{
super(str); //str becomes name of your thread
}
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(getName());
}
}
}
public class Thread2
{
public static void main(String args[])
{
SimpleThread t1=new SimpleThread("XXXXX");
SimpleThread t2=new SimpleThread("YYYYY");
t1.start();
t2.start();
}
}
Ouput
Note
If threads created in your program are not given any user defined names then JVM provides default thread name as follows.
Thread-0
Thread-1
Thread-2
Thread-3
Thread-4
Thread-5
----------so on.
Hi Friends, Please comment down your views in the comment section below. Thank you...
Continue to the next topic Runnable interface in Multi-threading.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
No comments:
Post a Comment