سؤال

Basically, if I have lots of synchronised methods in a monitor. Will this effectively avoid deadlocks?

هل كانت مفيدة؟

المحلول

In general, no, it does not guarantee the absence of the deadlocks. Please have a look at the code examples at Deadlocks and Synchronized methods and Deadlock in Java. The two classes, A and B, with synchronized methods only generate a perfect deadlock.

Also, in my opinion , your wording "Java monitor with Synchronised Methods", although being conceptually correct, slightly deviates from the one accepted in Java. For example the java.lang.Object.wait() javadoc puts in the following way :

"The current thread must own this object's monitor"

That implicitly suggests that the object and the monitor are not the same thing. Instead, the monitor is something we don't directly see or address.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top