DEPARTMENTOFINFORMAITON TECHNOLOGY
Multi Threading in Java
Class: [Link]. (IT) 4th Semester
Subject: OOPS with Java (BCS-403)
By: Mr. Dev Agrawal, Asst. Professor, Dept. of IT
Thread in Java
• A Java thread is the smallest unit of execution
within a program.
• It is a lightweight sub process that runs
independently but shares the same memory
space as the process, allowing multiple tasks to
execute concurrently.
Exception Handling in Java
Multithreading
• Multithreading in Java is a feature that allows
multiple tasks to run concurrently within the
same program.
• Instead of executing one task at a time, Java
enables parallel execution using lightweight
threads.
Exception Handling in Java
Methods in Thread Class
• start() Method
• suspend() Method
• stop() Method
• wait() Method
• notify() Method
• notifyAll() Method
• sleep() Method
• join() Method
Exception Handling in Java
Thread Life Cycle
Exception Handling in Java
Create Thread in Java
Exception Handling in Java
import [Link].*;
import [Link].*;
class MyThread extends Thread{
// initiated run method for Thread
public void run(){
[Link]("Thread Started Running...");
}
}
public class mainclass{
public static void main(String [] args) {
MyThread t1 = new MyThread();
[Link]();
[Link]();
}
}
Exception Handling in Java
import [Link].*;
import [Link].*;
class MyThread implements Runnable{
// Method to start Thread
public void run(){
[Link]("Thread is Running Successfully");
}
}
public class mainclass{
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]();
[Link](); // Method of runnable
}
}
Exception Handling in Java
Thread Priorities in Java
• Every thread in Java is assigned a priority that helps the
thread scheduler decide the order of execution when
multiple threads are waiting to run.
• Thread priority is represented by an integer value
ranging from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY),
with the default value being 5 (NORM_PRIORITY).
• The JVM thread scheduler uses these priorities as hints,
but actual scheduling depends on the operating system.
Exception Handling in Java
Inter-Thread Communication in Java
• In multithreading in Java, sometimes threads need to
communicate with each other to work together.
• Java provides built-in methods like wait(), notify(),
and notifyAll() to achieve smooth coordination
between threads.
• These methods help avoid problems such as busy
waiting and allow threads to signal each other when a
task is complete.
Exception Handling in Java
Assignment 2.2
1. Describe the Thread lifecycle.
2. Describe the inter-thread communication.
3. What are the thread priorities in java. Explain.
4. Discus the Thread Synchronization.
Exception Handling in Java