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 [Link] 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 [Link] 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(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
[Link]();
}
}
Output: thread is running...
Simple Thread Program
2) Java Thread Example by implementing Runnable interface
class Multi3 implements Runnable{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
[Link](); } }
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, [Link], Programming with Java, Tata McGraw Hill Companies
3. John P. Flynt Thomson, Java Programming.
Video Links:
[Link]
[Link]
Reference Links:
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
THANK YOU