Lifecycle and States of A Thread in Java
Lifecycle and States of A Thread in Java
A thread in Java at any point of time exists in any one of the following states. A thread lies only in one
of the shown states at any instant:
1. New State
2. Runnable State
3. Blocked State
4. Waiting State
5. Timed Waiting State
6. Terminated State
The diagram shown below represents various states of a thread at any instant in time.
Because it exits normally. This happens when the code of the thread has been
entirely executed by the program.
Because there occurred some unusual erroneous event, like a segmentation fault or
an unhandled exception.
Implementing the Thread States in Java
In Java, to get the current state of the thread, use Thread.getState() method to get the current state
of the thread. Java provides java.lang.Thread.State class that defines the ENUM constants for the
state of a thread, as a summary of which is given below:
1. New
Thread state for a thread that has not yet started.
public static final Thread.State NEW
2. Runnable
Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual
machine but it may be waiting for other resources from the operating system such as a processor.
public static final Thread.State RUNNABLE
3. Blocked
Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting
for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method
after calling Object.wait().
public static final Thread.State BLOCKED
4. Waiting
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following
methods:
Object.wait with no timeout
Thread.join with no timeout
LockSupport.park
public static final Thread.State WAITING
5. Timed Waiting
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state
due to calling one of the following methods with a specified positive waiting time:
Thread.sleep
Object.wait with timeout
Thread.join with timeout
LockSupport.parkNanos
LockSupport.parkUntil
public static final Thread.State TIMED_WAITING
6. Terminated
Thread state for a terminated thread. The thread has completed execution.
Declaration: public static final Thread.State TERMINATED
Note:
Java applet is deprecated because it’s no longer widely used on the web. The popularity of applets
has decreased over the years as browser support for applets has declined, and more advanced
technologies such as web-based applications and JavaScript have become more prevalent.
Additionally, applets are considered a security risk as they can execute arbitrary code on the client
machine, and many browsers now disable them by default. As a result, Java’s applet technology is
no longer seen as a valuable feature for Java developers and is removed from the newer versions of
Java.
Let us do see a hierarchy of Applet before landing up on stages in the lifecycle of the java applet that
is as follows in the below media:
Initializing an Applet
java.awt.applet.*;
Step 1: Initialization
There is no main method unlike our normal java programs. Every Applet will start it’s execution from
init() method. It is executed only once
Step 2: Start
public void start()
After init() method start() method is invoked. Executed when the browser is maximized
Step 3: Paint
Paint method is used to display the content on the applet. We can create the objects or components
to the applet or we can directly write a message on the applet. It will take Graphics class as a
parameter.
Step 4: Stop
stop() method is used to stop the applet. It is executed when the browser is minimized.
Step 5: Destroy
destroy() method is used to completely close the applet. It is executed when the applet is closed.
Implementation:
<HTML>
<applet>
code,width,height
</applet>
</HTML>
Note: Drawbacks of using HTML file is you need a plugin (java plugin) to run it on your browser.
1. init()
2. start()
3. paint()
4. stop()
5. destroy()
All these are available in AWT Package java.awt.applet.* and in order ton import paint (Graphics g)
we do use java.awt.component package
Method 1: init()
This method can be called only once during the run time of the applet
Syntax:
// To initialize objects
Method 2: start()
It is also called to restart an applet after it has been stopped. i.e. to resume the applet
Syntax:
Note: init() is called once i.e. when the first time an applet is loaded whereas start( ) is called each
time an applet’s HTML document is displayed onscreen.
Method 3: paint()
paint() method is used for painting any shapes like square, rectangle, trapeziums, etc.
paint() method has one parameter of type Graphics Class, this Graphics class enables the
painting features in an applet.
This parameter will contain the graphics context, which is used whenever output for the
applet is required.
Syntax:
Note: This is the only method among all the method mention above, which is parameterized.
Method 4: stop()
It is invoked every time the browser is stopped, minimized or when there is an abrupt failure
in the application.
After stop()method called, we can also use start() method whenever we want.
The stop( ) method is called when a web browser leaves the HTML document containing the
applet when it goes to another page, for example, when stop( ) is called, the applet is
probably running. You should use stop( ) to suspend threads that don’t need to run when the
applet is not visible. You can restart them when start( ) is called if the user returns to the
page.
Syntax:
Method 5: destroy()
destroy() method is used to destroy the application once we are done with our applet work.
It can be invoked only once.
Once applet is destroyed we can’t start() the applet (we cannot restore the applet again)
The destroy( ) method is called when the environment determines that your applet needs to
be removed completely from memory.
Syntax:
import java.io.*;
import java.util.*;