Java thread life cycle

3,903 views 6 slides Dec 19, 2020
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

LIFE CYCLE OF JAVA THREAD


Slide Content

LIFE CYCLE OF A
THREAD

CONTENT




S
NO
TOPIC
PAGE NO

1.

INTRODUCTION TO MULTITHREADED
PROGRAMMING

1

2.

LIFE CYCLE OF A THREAD 1

3.

STATES OF A THREAD 2

4.

THREAD PRIORITIES 3

5.

CREATE A THREAD BY IMPLEMENTING A
RUNNABLE INTERFACE

3

6.

BASIC METHODS

4

7.

BENEFITS OF MULTITHREADING 4

1 GO BACK
INTRODUCTION TO MULTITHREADED PROGRAMMING
Java is a multi-threaded programming language which means we can develop
multi-threaded program using Java.
A multi-threaded program contains two or more parts that can run concurrently and
each part can handle a different task at the same time.
By definition, multitasking is when multiple processes share common processing
resources such as a CPU.
Multi-threading extends the idea of multitasking into applications where you can
subdivide specific operations within a single application into individual threads.
Each of the threads can run in parallel. The OS divides processing time not only
among different applications, but also among each thread within an application.
Multi-threading enables you to write in a way where multiple activities can proceed
concurrently in the same program.
LIFE CYCLE OF A THREAD
A thread goes through various stages in its life cycle. For example, a thread is born,
started, runs, and then dies.
The following diagram shows the complete life cycle of a thread.

2 GO BACK
STATES OF A THREAD

A thread can be in any of the five following states

1. Newborn State: When a thread object is created a new thread is born
and said to be in Newborn state.

a. Runnable State: If a thread is in this state it means that the thread is
ready for execution and waiting for the availability of the processor. If all threads
in queue are of same priority then they are given time slots for execution in round
robin fashion

2. Running State: It means that the processor has given its time to the
thread for execution. A thread keeps running until the following conditions
occurs
a. Thread give up its control on its own and it can happen in the
following situations
i. A thread gets suspended using suspend() method which
can only be revived with resume() method

ii. A thread is made to sleep for a specified period of time using
sleep(time) method, where time in milliseconds

iii. A thread is made to wait for some event to occur using wait ()
method. In this case a thread can be scheduled to run again
using notify () method.
b. A thread is pre-empted by a higher priority thread

3. Blocked State: If a thread is prevented from entering into runnable
state and subsequently running state, then a thread is said to be in Blocked state.

4. Dead State: A runnable thread enters the Dead or terminated state
when it completes its task or otherwise terminates.

3 GO BACK
THREAD PRIORITIES
Every Java thread has a priority that helps the operating system determine the order
in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1)
and MAX_PRIORITY (a constant of 10). By default, every thread is given priority
NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should be
allocated processor time before lower-priority threads.
However, thread priorities cannot guarantee the order in which threads execute and
are very much platform dependent.


CREATE A THREAD BY IMPLEMENTING A RUNNABLE INTERFACE

If your class is intended to be executed as a thread then you can achieve this by
implementing a Runnable interface.
There are three basic steps
Step 1:
As a first step, you need to implement a run() method provided by
a Runnable interface. This method provides an entry point for the thread and you will put
your complete business logic inside this method. Following is a simple syntax of the run()
method
Public void run( );

Step 2:
As a second step, you will instantiate a Thread object using the following
constructor

Thread(Runnable threadObj, String threadName);

Where, threadObj is an instance of a class that implements the Runnable interface
and threadName is the name given to the new thread.
Step 3:
Once a Thread object is created, you can start it by calling start() method, which
executes a call to run( ) method. Following is a simple syntax of start() method


void start();

4 GO BACK
BASIC METHODS

Method Description

start() This method starts the execution of the thread and
JVM calls the run() method on the thread.

Sleep(int
milliseconds)
This method makes the thread sleep hence the thread's
execution will pause for milliseconds provided and
after that, again the thread starts executing. This help in
synchronization of the threads.

getName() It returns the name of the thread.

setPriority(int
newpriority)
It changes the priority of the thread.

yield () It causes current thread on halt and other threads to execute.



BENEFITS OF MULTITHREADING

1. Enables programmers to do multiple things at one time


\ 2. Programmers can divide a long program into threads and execute t
hem in parallel which eventually increases the speed of the program
execution

3. Improved performance and concurrency

4. Simultaneous access to multiple applications
Tags