THREAD IN JAVA
Life Cycle of a Thread
A thread in Java can be in one of the following states:
1. New
2. Runnable
3. Running
4. Blocked
5. Waiting
6. Timed Waiting
7. Terminated
New
A newly created thread that has not yet started execution.
Remains in this state until the program starts the thread using the start() method.
Also referred to as a born thread.
Runnable
Indicates that the thread is ready for execution and waiting for the availability of the
processor.
If all threads in the queue have the same priority, they are given time slots for
execution in a round-robin fashion.
Running
The processor has allocated time to the thread for execution.
A thread remains running until one of the following conditions occurs:
o The thread gives up its control voluntarily:
By being suspended using the suspend() method, which can only be
revived using the resume() method.
By being put to sleep for a specified period using the sleep(time)
method, where time is specified in milliseconds.
By waiting for some event to occur using the wait() method. In this
case, the thread can be scheduled to run again using the notify()
method.
o The thread is pre-empted by a higher priority thread.
Blocked
The thread is prevented from entering the runnable state or is waiting for I/O to
complete.
A thread in this state cannot continue its execution until it is moved to the runnable
state.
Life Cycle of a Thread
A thread in Java can be in one of the following states:
1. New
2. Runnable
3. Running
4. Blocked
5. Waiting
6. Timed Waiting
7. Terminated
New
A newly created thread that has not yet started execution.
Remains in this state until the program starts the thread using the start() method.
Also referred to as a born thread.
Runnable
Indicates that the thread is ready for execution and waiting for the availability of the
processor.
If all threads in the queue have the same priority, they are given time slots for
execution in a round-robin fashion.
Running
The processor has allocated time to the thread for execution.
A thread remains running until one of the following conditions occurs:
o The thread gives up its control voluntarily:
By being suspended using the suspend() method, which can only be
revived using the resume() method.
By being put to sleep for a specified period using the sleep(time)
method, where time is specified in milliseconds.
By waiting for some event to occur using the wait() method. In this
case, the thread can be scheduled to run again using the notify()
method.
o The thread is pre-empted by a higher priority thread.
Blocked
The thread is prevented from entering the runnable state or is waiting for I/O to
complete.
A thread in this state cannot continue its execution until it is moved to the runnable
state.