(maintained by Lakshmi Sarvani Videla, Former Assistant Professor, KLEF and now Computer Science Lecturer, SRR and CVR Government Degree College, Vijayawada
Monday, 25 December 2023
Wednesday, 6 December 2023
life cycle of a thread notes
A thread life cycle is always in one of these five states. It can move from one state to another state. In Java, the life cycle of thread has five states.
1. Newborn State
2. Runnable State
3. Running State
4. Blocked State
5. Dead State
New : A thread begins its life cycle in the new state. It remains in this state until the start() method is called on it.
Thread t = new Thread();Runnable : After invocation of start() method on new thread, the thread becomes runnable.
Thread t = new Thread();
t.start();Running : A thread is in running state if the thread scheduler has selected it.
t1.isAlive() - The isAlive() method returns true if the thread upon which it is called is still running otherwise it returns false.
Waiting : A thread is in waiting state if it waits for another thread to perform a task. In this stage the thread is still alive.
Example1: t1.join() waiting for t1 to complete its execution
Example2: The state of thread t1 after invoking the method sleep() on it - TIMED_WAITING
Thread.sleep(400);
Terminated : A thread enters the terminated state when it completes its task.
t1.stop()