rspec/rules/S2235/java/rule.adoc

54 lines
1016 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public void doSomething(){
...
try {
...
anObject.notify();
...
} catch(IllegalMonitorStateException e) {
...
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public void doSomething(){
...
synchronized(anObject) {
...
anObject.notify();
...
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]