0% found this document useful (0 votes)
4 views7 pages

Java Thread States Explained

Uploaded by

jattubhai0509
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Thread States Explained

Uploaded by

jattubhai0509
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

•New State

As we use the Thread class to construct a thread entity, the thread is born and is defined as being in the
New state. That is, when a thread is created, it enters a new state, but the start() method on the instance
has not yet been invoked.

•Runnable State
A thread in the runnable state is prepared to execute the code. When a new thread's start() function is
called, it enters a runnable state.
In the runnable environment, the thread is ready for execution and is awaiting the processor's
availability (CPU time). That is, the thread has entered the queue (line) of threads waiting for execution

•Running State
Running implies that the processor (CPU) has assigned a time slot to the thread for execution. When a
thread from the runnable state is chosen for execution by the thread scheduler, it joins the running state.
In the running state, the processor allots time to the thread for execution and runs its run procedure. This
is the state in which the thread directly executes its operations. Only from the runnable state will a thread
enter the running state.
•Blocked State
When the thread is alive, i.e., the thread class object persists, but it cannot be selected for execution
by the scheduler. It is now inactive.

•Dead State
When a thread's run() function ends the execution of sentences, it automatically dies or enters the
dead state. That is, when a thread exits the run() process, it is terminated or killed. When the stop()
function is invoked, a thread will also go dead.
Thread Class and Runnable Interface
Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable. To
create a new thread, your program will either extend Thread or implement the Runnable interface.
The Thread class defines several methods that help manage [Link] table below displays the same:

Now let us see how to use a Thread which begins with the main java thread, that all Java programs have.
Main Java Thread
Now let us see how to use Thread and Runnable interface to create and manage threads, beginning with the main java
thread, that all Java programs have. So, let us discuss the main thread.

Why is Main Thread so important?


•Because this thread effects the other ‘child’ threads
•Because it performs various shutdown actions
•It is created automatically when your program is started.
How to Create a Java Thread?
Java lets you create thread in following two ways:-
•By implementing the Runnable interface.
•By extending the Thread
Let’s see how both the ways help in implementing Java thread.
Runnable Interface
The easiest way to create a thread is to create a class that implements the Runnable interface.
To implement Runnable interface, a class need only implement a single method called run( ), which is declared like
this:

You might also like