rspec/rules/S2276/java/rule.adoc
2022-02-04 16:28:24 +00:00

55 lines
1.1 KiB
Plaintext

If ``++Thread.sleep(...)++`` is called when the current thread holds a lock, it could lead to performance and scalability issues, or even worse to deadlocks because the execution of the thread holding the lock is frozen. It's better to call ``++wait(...)++`` on the monitor object to temporarily release the lock and allow other threads to run.
== Noncompliant Code Example
[source,java]
----
public void doSomething(){
synchronized(monitor) {
while(notReady()){
Thread.sleep(200);
}
process();
}
...
}
----
== Compliant Solution
[source,java]
----
public void doSomething(){
synchronized(monitor) {
while(notReady()){
monitor.wait(200);
}
process();
}
...
}
----
== See
* https://wiki.sei.cmu.edu/confluence/x/YTdGBQ[CERT, LCK09-J.] - Do not perform operations that can block while holding a lock
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]