0% found this document useful (0 votes)
27 views19 pages

Lecture 2.1.3

The document provides an overview of multithreading in Java, including its advantages and disadvantages, as well as the life cycle of a thread. It explains how to create threads using the Thread class and the Runnable interface, along with examples of each. Additionally, it includes references for further learning on the topic.

Uploaded by

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

Lecture 2.1.3

The document provides an overview of multithreading in Java, including its advantages and disadvantages, as well as the life cycle of a thread. It explains how to create threads using the Thread class and the Runnable interface, along with examples of each. Additionally, it includes references for further learning on the topic.

Uploaded by

madangleaming13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)

TOPIC OF PRESENTATION:

Multithreading in Java. Thread Priority, Thread LifeCycle. (CO 3)

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


Multithreading in Java. Thread
Priority, Thread LifeCycle.

2
Multithreading
Multithreading is a Java feature that allows concurrent execution of two or more
parts of a program for maximum utilization of CPU.
Each part of such program is called a thread. So, threads are light-weight processes
within a process.
Use of Multithreading

• A multithreaded application performs two or more activities concurrently


• It is accomplished by having each activity performed by a separate thread
• Threads are the lightest tasks within a program, and they share memory space and
resources with each other
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.
Following are the stages of the life cycle −
New − A new thread begins its life cycle in the new state. It remains in this state
until the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread
waits for another thread to perform a task. A thread transitions back to the
runnable state only when another thread signals the waiting thread to continue
executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when
that time interval expires or when the event it is waiting for occurs.
Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.
Advantages of a Multithreaded Application

•Improved performance and concurrency


•Simplified coding of remote procedure calls and conversations
•Simultaneous access to multiple applications
•Reduced number of required servers
Disadvantages of a Multithreaded Application

Multithreaded applications present the following disadvantages:

•Difficulty of writing code


•Difficulty of debugging
•Difficulty of managing concurrency
•Difficulty of testing
•Difficulty of porting existing code
How to create thread
There are two ways to create a thread:
• By extending Thread class
• By implementing Runnable interface.
Thread class
Thread class provide constructors and methods to create and perform operations on a
thread. Thread class extends Object class and implements Runnable interface.

Commonly used Constructors of Thread class:

Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
Methods of Thread class:

public void run(): is used to perform action for a thread.


public void start(): starts the execution of the thread.JVM calls the run() method on
the thread.
public void sleep(long miliseconds): Causes the currently executing thread to sleep
(temporarily cease execution) for the specified number of milliseconds.
public void join(): waits for a thread to die.
public void join(long miliseconds): waits for a thread to die for the specified
miliseconds.
public int getPriority(): returns the priority of the thread.
public int setPriority(int priority): changes the priority of the thread.
public String getName(): returns the name of the thread.
public void setName(String name): changes the name of the thread.
public Thread currentThread(): returns the reference of currently executing
thread.
public int getId(): returns the id of the thread.
public Thread.State getState(): returns the state of the thread.
public boolean isAlive(): tests if the thread is alive.
public void yield(): causes the currently executing thread object to temporarily
pause and allow other threads to execute.
public void suspend(): is used to suspend the thread(depricated).
public void resume(): is used to resume the suspended thread(depricated).
public void stop(): is used to stop the thread(depricated).
public boolean isDaemon(): tests if the thread is a daemon thread.
public void setDaemon(boolean b): marks the thread as daemon or user thread.
public void interrupt(): interrupts the thread.
public boolean isInterrupted(): tests if the thread has been interrupted.
public static boolean interrupted(): tests if the current thread has been interrupted.
Runnable interface:
The Runnable interface should be implemented by any class whose instances are
intended to be executed by a thread.
Runnable interface have only one method named run().
public void run(): is used to perform action for a thread.

Starting a thread:
start() method of Thread class is used to start a newly created thread. It performs
following tasks:
• A new thread starts(with new callstack).
• The thread moves from New state to the Runnable state.
• When the thread gets a chance to execute, its target run() method will run.
Simple Thread Program
1) Java Thread Example by extending Thread class
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Output: thread is running...
Simple Thread Program
2) Java Thread Example by implementing Runnable interface
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start(); } }

Output: thread is running...


Summary:

In this session, you were able to :

•Learn about Multithreading


•Understand Thread Life cycle
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill Companies
3. John P. Flynt Thomson, Java Programming.

Video Links:
https://youtu.be/O_Ojfq-OIpM
https://youtu.be/JceAHRlQsqc

Reference Links:
https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/
https://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java
https://www.tutorialspoint.com/java/java_multithreading.htm
https://www.javatpoint.com/multithreading-in-java
https://www.journaldev.com/1079/multithreading-in-java
https://www.javatpoint.com/join()-method
THANK YOU

You might also like