Holding locks while performing time-consuming or blocking operations can severely degrade system performance and can result in starvation. Furthermore, deadlock can result if interdependent threads block indefinitely. Blocking operations include network, file, and console I/O (for example, Console.readLine()
) and object serialization. Deferring a thread indefinitely also constitutes a blocking operation. Consequently, programs must not perform blocking operations while holding a lock.
When the Java Virtual Machine (JVM) interacts with a file system that operates over an unreliable network, file I/O might incur a large performance penalty. In such cases, avoid file I/O over the network while holding a lock. File operations (such as logging) that could block while waiting for the output stream lock or for I/O to complete could be performed in a dedicated thread to speed up task processing. Logging requests can be added to a queue, assuming that the queue's put()
operation incurs little overhead as compared to file I/O [Goetz 2006].
...
This noncompliant code example defines a utility method that accepts a time
argument.:
Code Block | ||
---|---|---|
| ||
public synchronized void doSomething(long time) throws InterruptedException { // ... Thread.sleep(time); } |
Because the method is synchronized, when the thread is suspended, other threads cannot use the synchronized methods of the class. The current object's monitor continues to be held because the Thread.sleep()
method lacks synchronization semantics.
Compliant Solution (Intrinsic Lock)
This compliant solution defines the doSomething()
method with a timeout
parameter rather than the time
value. Using Object.wait()
instead of Thread.sleep()
allows setting a time out timeout period during which a notification may awaken the thread.
...
The current object's monitor is immediately released upon entering the wait state. After When the timeout period has elapsedelapses, the thread resumes execution after reacquiring the current object's monitor.
According to the Java API class Class Object
documentation [API 20062014]
Note that the
wait
method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.This method should only be called by a thread that is the owner of this object's monitor.
Programs must ensure that threads that hold locks on other objects release those locks appropriately before entering the wait state. Additional guidance on waiting and notification is available in rules THI03-J. Always invoke wait() and await() methods inside a loop and THI02-J. Notify all waiting threads rather than a single thread.
...
Calling writeObject()
within the synchronized sendPage()
method can result in delays and deadlock-like conditions in high-latency networks or when network connections are inherently lossy.
...
- Perform actions on data structures requiring synchronization.
- Create copies of the objects to be sent.
- Perform network calls in a separate unsynchronized method.
...
LCK09-EX0: Classes that provide an appropriate termination mechanism to callers are permitted to violate this rule . See rule (see THI04-J. Ensure that threads performing blocking operations can be terminated).
LCK09-EX1: Methods that require multiple locks may hold several locks while waiting for the remaining locks to become available. This constitutes a valid exception, although the programmer must follow other applicable rules, especially rule LCK07-J. Avoid deadlock by requesting and releasing locks in the same order to avoid deadlock .
Automated Detection
Some static analysis tools are capable of detecting violations of this rule.
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
ThreadSafe |
| CCE_LK_LOCKED_BLOCKING_CALLS | Implemented |
Risk Assessment
Blocking or lengthy operations performed within synchronized regions could result in a deadlocked or unresponsive system.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
LCK09-J | lowLow | probableProbable | highHigh | P2 | L3 |
Related Guidelines
Bibliography
...