Frage

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

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top