Introduction
In this article, we discuss the life cycle of a thread in Java.
What is Thread
A thread is a single sequential flow of control in a program. Sometimes called a lightweight process, so there are 2 different definitions for explaining this topic.
States of threads
A thread life cycle contains the following states:
- New State
- Runnable State
- Running State
- Blocked (Non-Runnable) State
- Terminated State

1. Initializing Thread
When we start a thread it goes into the new state.
In this state, the thread is only created but not working.
We can move the thread to the running state by invoking the "start()" method & the thread can be killed by the "stop()" method.
Syntax
Thread thread = new Thread;
Example
In this example we create a simple thread; we use the "sleep()" method for delaying the thread for execution.
- public class ThreadEx {
- public static void main(String args[]) throws Throwable {
- new ThreadEx().threadfunctn();
- }
- void threadfunctn() throws Throwable {
- for (int i = 0; i < 10; i++) {
- System.out.println("thread in " + Thread.currentThread().getName() + " process " + i);
- Thread.sleep(500);
- }
- }
- }

2. Runnable state of Thread
In this state Thread is in the running mode & waiting for control input.
We can move control to another thread by the "yield()" method.
Example
In this example we create two parallel threads using the "runnable()" interface, in other words using a "run()" method.
- public class RunnableEx implements Runnable {
- public static void main(String args[]) throws Throwable {
- RunnableEx run1 = new RunnableEx();
- RunnableEx run2 = new RunnableEx();
- new Thread(run1).start();
- new Thread(run2).start();
- // Our main thread is stopped here,
- // new thread-0 and new thread-1 are continued....
- }
- public void run() {
- try {
- for (int i = 0; i < 10; i++) {
- System.out.println("In thread " + Thread.currentThread().getName() + " processed " + i);
- Thread.sleep(500);
- }
- } catch (Throwable tr) {}
- }
- }




Join the conversation! Your thoughts help the community grow.