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.
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, thenDSA, and finallySpringBoot. - Student-2 tries to acquire
SpringBoot, thenDSA, and finallyJava.
Since both threads hold one resource and wait for the other, a deadlock occurs.
Libraryclass implementsRunnable.- Shared resources:
res1,res2,res3. - Threads:
Student-1andStudent-2. - Demonstrates synchronized blocks and thread locking.
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)
To avoid deadlock:
- Always acquire locks in the same order in all threads.
- Use tryLock() (from
java.util.concurrent.locks) with timeout to avoid indefinite waiting. - Minimize nested synchronized blocks.
- Language: Java
- IDE: IntelliJ IDEA / VS Code
- Concepts: Multithreading, Synchronization, Deadlock, Runnable Interface
Raju Chowdhury
B.Tech CSE, Swami Vivekananda University
Aspiring Java Backend Developer