Programming Languages Java Subjective
Sep 24, 2025

Explain deadlock in Java and how to prevent it.

Detailed Explanation
Deadlock occurs when two or more threads are blocked forever, waiting for each other. Prevention methods: 1) Avoid nested locks, 2) Avoid unnecessary locks, 3) Use timeout with tryLock(), 4) Lock ordering (always acquire locks in same order), 5) Use concurrent collections. Example: Thread1 locks A then B, Thread2 locks B then A - this can cause deadlock.
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback