Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Life Cycle of Thread in Core Java

In our previous video of our ongoing Core Java course we have learn about Entry Interface and Hash Map class in Java programming language. In this video we are going to see that what is Life Cycle of Thread in Java.

Normally in a Java thread there can be five states. But according to SUN microsystems, there is only four states in thread life cycle in Java. The Java thread states are as follows :
1. New
2. Runnable
3. Running
4. Non-runnable (blocked)
5. Terminated

The Life Cycle of thread in Java is controlled by JVM (Java Virtual Machine).

New : The thread is in new state if you create an instance of thread class but before the invocation of start() method.

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.

Running : The thread is in running state if the thread scheduler has selected it.

Non-Runnable (Blocked) : This is the state when the thread is still alive, but is currently not eligible to run.

Terminated : The thread is in terminated or dead state when its run() method exits.

A Thread in Java can be created by extending thread class and by implementing Runnable interface in Java.

Thread class : Thread class provide constructors and methods to create and perform operations on a thread. It extends Object class and implements Runnable interface.

Starting a thread : start() method of Thread class is used to start a newly created thread. The thread moves from New state to the Runnable state. When the thread gets a chance to execute, its target run() method will run.
Example :
class Multi exntends Thread
{
void run()
{
System.out.println("Thread is running");
}
public static void main(String args[])
{
Multi m=new Multi();
m.run();
}
}
Output :
Thread is running

The runnable interface in Java should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().
Example :
class multil implements Runnable
{
public void run()
{
System.out.println("hi this my new thread");
}
public static void main (String s[])
{
multil mm=new multil();
mm.run();
}
}
Output :
hy this my new thread

The sleep() method of Thread class in Java is used to sleep a thread for the specified amount of time.
Example :
class TestSleep extends Thread
{
public void run()
{
for(int i=1;i<5;i++)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{System.out.println (e);} System.out.println(i);
}
}
public static void main(String args[])
{
TestSleep t1=new TestSleep();
Test sleep t2=new TestSleep();
t1.start();
t2.start();
}
}
Output :
1
1
2
2
3
3
4
4
(The output will be printed in slow motion(with some rest)).

So, this is how Life Cycle of Thread in Java programming works. We also learnt about states of Java thread. Now in the next video we will see Introduction about Object Oriented Programming in Java programming language.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere