rspec/rules/S2236/java/rule.adoc

29 lines
1022 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The methods ``++wait(...)++``, ``++notify()++`` and ``++notifyAll()++`` are available on a ``++Thread++`` instance, but only because all classes in Java extend ``++Object++`` and therefore automatically inherit those methods. But there are two very good reasons for not calling them on a ``++Thread++``:
* Internally, the JVM relies on these methods to change the state of the Thread (``++BLOCKED++``, ``++WAITING++``, ...), so calling them will corrupt the behavior of the JVM.
* It is not clear (perhaps even to the original coder) what is really expected. For instance, it is waiting for the execution of the Thread to suspended, or is it the acquisition of the object monitor that is waited for?
=== 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
----
Thread myThread = new Thread(new RunnableJob());
...
myThread.wait(2000);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]