2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-06-08 17:20:36 +02:00
|
|
|
In Java, the `Thread` class represents a thread of execution.
|
|
|
|
Synchronization between threads is typically achieved using objects or shared resources.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-08 17:20:36 +02:00
|
|
|
The methods `wait(...)`, `notify()`, and `notifyAll()` are related to the underlying object's monitor
|
|
|
|
and are designed to be called on objects that act as locks or monitors for synchronization.
|
|
|
|
These methods are available on Java `Object` and, therefore, automatically inherited by all objects, including `Thread.`
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-08 17:20:36 +02:00
|
|
|
Calling these methods on a `Thread` may corrupt the behavior of the JVM, which relies on them to change the state
|
|
|
|
of the thread (`BLOCKED,` `WAITING,`...).
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== 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());
|
|
|
|
...
|
2023-06-08 17:20:36 +02:00
|
|
|
myThread.wait(); // Noncompliant
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-06-08 17:20:36 +02:00
|
|
|
== Resources
|
|
|
|
|
|
|
|
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Thread.html[Oracle Java SE - Thread]
|
|
|
|
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Object.html[Oracle Java SE - Object]
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Refactor the synchronisation mechanism to not use a Thread instance as a monitor.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|