
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.
57 lines
1.0 KiB
Plaintext
57 lines
1.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
According to Oracle Javadoc:
|
|
|
|
____
|
|
``++IllegalMonitorStateException++`` is thrown when a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
|
|
|
|
____
|
|
|
|
In other words, this exception can be thrown only in case of bad design because ``++Object.wait(...)++``, ``++Object.notify()++`` and ``++Object.notifyAll()++`` methods should never be called on an object whose monitor is not held.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public void doSomething(){
|
|
...
|
|
try {
|
|
...
|
|
anObject.notify();
|
|
...
|
|
} catch(IllegalMonitorStateException e) {
|
|
...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public void doSomething(){
|
|
...
|
|
synchronized(anObject) {
|
|
...
|
|
anObject.notify();
|
|
...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Refactor this code to not catch IllegalMonitorStateException.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|