Lecture 2.1.3
Lecture 2.1.3
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)
TOPIC OF PRESENTATION:
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
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
Methods of Thread class:
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(); } }
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