How would you avoid deadlocks in Java ?

Showing Answers 1 - 3 of 3 Answers


  • Nov 21st, 2011

To avoid deadlocks you can use a condition object. Condition objects allow a thread to temporarily release a
lock, so that another thread can proceed, and to regain the lock at a later time.

Calling await on a condition object makes the current thread wait and allows another thread to acquire the lock object.

A waiting thread is blocked until another thread calls signalAll or signal on the condition object for which the thread is waiting.

  Was this answer useful?  Yes


  • Nov 28th, 2011

synchronization can cause deadlocks when two
threads are waiting on each other to do something. Also synchronized code has the overhead of acquiring lock,
which can adversely affect the performance.

ThreadLocal is a handy class for simplifying development of thread-safe

concurrent programs by making the object stored in this class not sharable between threads. ThreadLocal class
encapsulates non-thread-safe classes to be safely used in a multi-threaded environment and also allows you to
create per-thread-singleton.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions