Java Threading and Multithreading
Slide 1: Introduction
• Thread: A lightweight sub-process, smallest unit of execution
• Multithreading: Executing multiple threads simultaneously within a program
• Improves performance, responsiveness, and resource utilization
• Widely used in servers, games, GUI applications, and real-time systems
Slide 2: Process vs Thread
• Process: Independent program with its own memory space
• Thread: Part of a process, shares memory with other threads
Process Thread
Heavyweight Lightweight
Separate memory Shared memory
Slow context switch Fast context switch
Slide 3: Creating Threads in Java
Two ways:
1. Extending Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread running");
}
}
2. Implementing Runnable interface (Preferred)
class MyTask implements Runnable {
public void run() {
[Link]("Task running");
}
}
1
Slide 4: Thread Lifecycle
Java thread goes through the following states: 1. New – Thread object created 2. Runnable – Ready to
run, waiting for CPU 3. Running – Thread is executing 4. Blocked / Waiting – Waiting for resource or
signal 5. Timed Waiting – Sleeping or waiting with timeout 6. Terminated (Dead) – Execution
completed
Slide 5: Thread Lifecycle Diagram (Explanation)
• start() → New → Runnable
• Scheduler selects → Running
• sleep() / wait() → Waiting
• notify() → Runnable
• run() finishes → Dead
Slide 6: Important Thread Methods
Method Description
start() Starts a new thread
run() Contains thread logic
sleep(ms) Pauses execution temporarily
wait() Releases lock and waits
notify() Wakes up waiting thread
join() Waits for another thread to finish
Slide 7: Effect of run(), sleep(), wait()
run()
• Executes thread logic
• If called directly, runs like a normal method (no new thread)
sleep()
• Pauses thread for fixed time
• Does not release lock
wait()
• Pauses thread until notified
• Releases lock
2
Slide 8: Example – sleep()
class SleepDemo extends Thread {
public void run() {
try {
[Link](1000);
[Link]("Thread woke up");
} catch (Exception e) {}
}
}
Slide 9: Multithreading Example
class IDThread extends Thread {
public void run() {
[Link]("ID: 101");
}
}
class NameThread extends Thread {
public void run() {
[Link]("Name: Ali");
}
}
Slide 10: Problems in Multithreading
• Race condition
• Inconsistent data
• Deadlock
Solution: Synchronization
Slide 11: Synchronization in Java
• Ensures only one thread accesses shared resource at a time
• Achieved using synchronized keyword
synchronized void display() {
// critical section
}
3
Slide 12: Coordinated Output Example
class Student {
synchronized void showID() {
[Link]("ID: 101");
}
synchronized void showName() {
[Link]("Name: Ali");
}
}
Slide 13: Inter-thread Communication
• Methods: wait() , notify() , notifyAll()
• Used for cooperation between threads
Example use cases: - Producer–Consumer problem - Shared resource management
Slide 14: Thread Priority
• Range: 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY)
• Default priority: 5
• Scheduler may prefer higher priority threads
Slide 15: Advantages of Multithreading
• Faster execution
• Better CPU utilization
• Responsive applications
• Simplified modeling of real-world problems
Slide 16: Disadvantages
• Complex debugging
• Risk of deadlock
• Difficult synchronization
• Increased overhead
Slide 17: Real-Life Applications
• Web servers
• Online banking systems
4
• Operating systems
• Games and simulations
Slide 18: Exam Tips
• Know thread lifecycle states
• Understand differences between sleep() and wait()
• Practice synchronization examples
• Remember: start() creates new thread, run() does not
Slide 19: Summary
• Thread = lightweight process
• Java supports multithreading
• Synchronization ensures data safety
• Proper design avoids concurrency issues
Slide 20: Thank You
Questions?