Multi Threading
Multi Threading
✅ Here, t1.start(); and t2.start(); run simultaneously (not one after another).
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
🔹 Why use synchronized ? It prevents both threads from modifying count at the
same time.
t1.start();
try {
t1.join(); // Main thread waits for t1 to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}
t1.setPriority(Thread.MIN_PRIORITY); // Priority 1
t2.setPriority(Thread.MAX_PRIORITY); // Priority 10
t1.start();
t2.start();
🔹 Higher priority threads may execute first but not always (depends on CPU
scheduling).
t1.start();
t2.start();
✅ Without synchronized , count might not reach 2000 due to race conditions.
🎯 Final Summary
✔ Multithreading allows multiple tasks to run simultaneously
✔ Threads have 5 states: New → Runnable → Running → Blocked → Terminated
✔ Important methods:
start() → Starts a thread
🔹 Basic Level
1️⃣ Create a simple thread using
Thread class
Runnable interface
2️⃣ Write a program where two threads print numbers from 1 to 10.
3️⃣ Modify the above program using so that even numbers print only after
join()
number from 1 to 5.
🔹 Intermediate Level
5️⃣ Create a multi-threaded program where:
Thread-1 prints A to Z
Thread-2 prints 1 to 26
🔹 Advanced Level
8️⃣ Implement a Producer-Consumer problem using multithreading and wait() &
notify() .
9️⃣ Create a program where multiple users (threads) try to book train tickets
(shared resource).
🚀
Synchronization in Java (With Diagram &
Examples)
🔹 What is Synchronization?
Synchronization in Java is used to control access to shared resources in a multi-
threaded environment to prevent race conditions. It ensures that only one thread
can access a critical section at a time.
👆 Here, both threads modify the same variable at the same time, leading to
inconsistent results.
t1.start();
t2.start();
}
}
method.
📌 Example: Ticket Booking System
class TicketCounter {
private int availableTickets = 5;
t1.start();
t2.start();
}
}
✅ Only the ticket booking process is synchronized. The print statements can
still run in parallel!
t1.start();
t2.start();
}
}
✅ Ensures only one document prints at a time, even across different objects!
🔥 4️⃣ Inter-Thread Communication ( wait() , notify() )
👉 Threads can "wait" and "notify" each other to coordinate execution.
📌 Example: Producer-Consumer Problem
class SharedResource {
private int item = 0;
private boolean available = false;
producer.start();
consumer.start();
🔹 Basic Level
1️⃣ Create a synchronized method in a class to allow two users to
BankAccount
3️⃣ Write a program where two threads print numbers from 1 to 10.
Thread-1: Prints odd numbers
🔹 Intermediate Level
4️⃣ Implement a ticket booking system where multiple users (threads) try to book
seats.
Use static synchronization so that only one document prints at a time, even
across different threads.
🔹 Advanced Level
7️⃣ Implement the Producer-Consumer problem using wait() and notify()
methods.
8️⃣ Create a multi-threaded ATM system where multiple users can:
Check balance
Deposit money
Withdraw money
Let me know if you need hints, solutions, or explanations for any question! 🚀😊