freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Thread Life cycle

1) New
2) Ready
3) Running state
4) Blocked / waiting / non-running mode
5) Dead state

Java Images

New :-
MyThread t=new MyThread();
Ready :-
t.start()
Running state:-
If thread scheduler allocates CPU for particular thread. Thread goes to running state The Thread is running state means the run() is executed.
Blocked State:-
If the running thread got interrupted of goes to sleeping state at that moment it goes to the blocked state.
Dead State:-
If the business logic of the project is completed means run() over thread goes dead state.

Second approach to create thread implementing Runnable interface:-

Step 1:-
Creates a class that implements Runnable interface.
class MyClass extends Runnable
{
public void run()
{
System.out.println("freetimestudy");
System.out.println("body of the thread");
}
};
Step 2:-
Creating a object.
MyClass obj=new MyClass();
Step 3:-
Creates a Thread class object.
Thread t=new Thread(obj);
Step 4:-
Starts the execution of a thread.
t.start();

implementing Runnable interface

class MyThread implements Runnable
{
public void run()
{
System.out.println("freetimestudy");
System.out.println("body of the thread");
}
}
class ThreadDemo
{
public static void main(String[] args)
{
MyClasss obj=new MyClass();
Thread t=new Thread(obj);
t.start();
}
}

intput and output

output:freetimestudy
body of the thread

Step 1:-
the Class MyClass implements the Runnable interface and overriding run() method and contains the logic associates with the body of the thread.
Step 2:-
Creates the object of implementation class this is not like a first mechanism.
Step 3 :-
Creates a generic thread object then pass the MyClass reference variable as a parameter to that object.
Step 4:-
As a result of third step 3 a thread object is created in order to execute this thread method we need to class start() method. Then new thread is executed.