
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
71 lines
1.3 KiB
Plaintext
71 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
By contract, the method ``++Object.wait(...)++``, ``++Object.notify()++`` and ``++Object.notifyAll()++`` should be called by a thread that is the owner of the object's monitor. If this is not the case an ``++IllegalMonitorStateException++`` exception is thrown. This rule reinforces this constraint by making it mandatory to call one of these methods only inside a ``++synchronized++`` method or statement.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
private void removeElement() {
|
|
while (!suitableCondition()){
|
|
obj.wait();
|
|
}
|
|
... // Perform removal
|
|
}
|
|
----
|
|
|
|
or
|
|
|
|
|
|
[source,java]
|
|
----
|
|
private void removeElement() {
|
|
while (!suitableCondition()){
|
|
wait();
|
|
}
|
|
... // Perform removal
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
private void removeElement() {
|
|
synchronized(obj) {
|
|
while (!suitableCondition()){
|
|
obj.wait();
|
|
}
|
|
... // Perform removal
|
|
}
|
|
}
|
|
----
|
|
|
|
or
|
|
|
|
|
|
[source,java]
|
|
----
|
|
private synchronized void removeElement() {
|
|
while (!suitableCondition()){
|
|
wait();
|
|
}
|
|
... // Perform removal
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Make this call to "[wait(...)|notify()|notifyAll()]" only inside a synchronized block to be sure to hold the monitor on "[this|xxx]" object.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|