Life cycle of a Thread:
A thread can be in one of the five states in the thread.According to sun, there is only 4 states new, runnable, non-runnable and terminated.
There is no running state. But for better understanding of the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are given below:
>1. New
>2. Runnable
>3. Running
>4. Non-Runnable (Blocked)
>5. Terminated
>1. New:
A thread is in new state if you create an instance of Thread class but before the invocation of start() method
i.e. Thread th=new Thread();
>2. Runnable:
The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.
i.e. th.start();
>3. Running:
The thread is in running state if the thread scheduler has selected it means run() method call on that thread.
i.e. th.start();
th.run();
>4. Non-Runnable (Blocked):
This is the state when the thread is still alive, but is currently not eligible to run.It is in blocked state or sleep or wait or in suspend state.
i.e. th.wait();
>5. Terminated:
A thread is in terminated or dead state when its run() method exits means on calling destroy() method
i.e. th.destroy();
Next--->How to create Thread in Java
A thread can be in one of the five states in the thread.According to sun, there is only 4 states new, runnable, non-runnable and terminated.
There is no running state. But for better understanding of the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are given below:
>1. New
>2. Runnable
>3. Running
>4. Non-Runnable (Blocked)
>5. Terminated
>1. New:
A thread is in new state if you create an instance of Thread class but before the invocation of start() method
i.e. Thread th=new Thread();
>2. Runnable:
The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.
i.e. th.start();
>3. Running:
The thread is in running state if the thread scheduler has selected it means run() method call on that thread.
i.e. th.start();
th.run();
>4. Non-Runnable (Blocked):
This is the state when the thread is still alive, but is currently not eligible to run.It is in blocked state or sleep or wait or in suspend state.
i.e. th.wait();
>5. Terminated:
A thread is in terminated or dead state when its run() method exits means on calling destroy() method
i.e. th.destroy();
Next--->How to create Thread in Java
No comments:
Post a Comment