Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What happens when a deadlock occurs in a multithreaded application?
A deadlock happens when two or more threads each hold a lock that another thread in the group needs, and each waits forever for the other to release it, forming a circular chain that never breaks on its own.
flowchart LR T1[Thread 1] -- holds --> LA[Lock A] T1 -- wants --> LB[Lock B] T2[Thread 2] -- holds --> LB T2 -- wants --> LA
Nothing crashes and no exception is thrown; the affected threads simply stop making progress permanently, which typically shows up as the application hanging or specific requests never completing while CPU usage on those threads drops to zero.
Classic deadlock requires four conditions to hold simultaneously: mutual exclusion (locks aren't shareable), hold-and-wait (a thread holds one lock while waiting for another), no preemption (a lock can't be forcibly taken away), and circular wait. Breaking any one of them, most commonly by always acquiring multiple locks in a fixed, consistent global order, prevents the deadlock.
More Related questions...