Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 1.68 KB

File metadata and controls

57 lines (42 loc) · 1.68 KB

MultiThreading-Synchronization-Deadlock-Java

📘 Overview

This project demonstrates a deadlock situation in Java using multithreading and synchronized blocks.
Two threads (students) attempt to acquire shared resources (books) in a conflicting order, which leads to a synchronization deadlock.


🧠 Concept

A deadlock occurs when two or more threads are waiting for each other to release resources, resulting in a situation where none of them can proceed.

In this example:

  • Student-1 tries to acquire Java, then DSA, and finally SpringBoot.
  • Student-2 tries to acquire SpringBoot, then DSA, and finally Java.

Since both threads hold one resource and wait for the other, a deadlock occurs.


🧩 Code Structure

  • Library class implements Runnable.
  • Shared resources: res1, res2, res3.
  • Threads: Student-1 and Student-2.
  • Demonstrates synchronized blocks and thread locking.

🧾 Example Output

Student-1 got into the library 
Student-1 has acquired Java
Student-2 got into the library 
Student-2 has acquired SpringBoot

(Execution will freeze here because of deadlock)


🚫 Deadlock Prevention Tip

To avoid deadlock:

  1. Always acquire locks in the same order in all threads.
  2. Use tryLock() (from java.util.concurrent.locks) with timeout to avoid indefinite waiting.
  3. Minimize nested synchronized blocks.

🧰 Technologies Used

  • Language: Java
  • IDE: IntelliJ IDEA / VS Code
  • Concepts: Multithreading, Synchronization, Deadlock, Runnable Interface

👨‍💻 Author

Raju Chowdhury
B.Tech CSE, Swami Vivekananda University
Aspiring Java Backend Developer