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 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 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 {{ Wiki Markup put()
}} operation incurs little overhead as compared to file I/O \ [[Goetz 2006|AA. References#Goetz 06]\].
Noncompliant Code Example (Deferring a Thread)
...
The current object's monitor is immediately released upon entering the wait state. After the timeout period has elapsed, the thread resumes execution after reacquiring the current object's monitor.unmigrated-wiki-markup
According to the Java API class {{Object
}} documentation \[ [API 2006|AA. References#API 06]\]
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.
...
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="592c303f-ea58-41e9-9a6d-5390dfce424c"><ac:plain-text-body><![CDATA[ | [ [API 2006AA. References#API 06] ] | Class | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="110b6f58-72d3-4e8c-a61a-0508e2fdf8a3"><ac:plain-text-body><![CDATA[ |
[ [Grosso 2001AA. References#Grosso 01]] | [Chapter 10, Serializationhttp://oreilly.com/catalog/javarmi/chapter/ch10.html] | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d4fa38e1-fa2a-4205-a964-ed17a4796aa6"><ac:plain-text-body><![CDATA[ | |
[ [JLS 2005AA. References#JLS 05]] | http://java.sun.com/docs/books/jls/third_edition/html/memory.html] | ]]></ac:plain-text-body></ac:structured-macro> | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d1775732-1190-4308-933e-f62dbe431116"><ac:plain-text-body><![CDATA[ | [ [Rotem 2008AA. References#Rotem 08]] | http://www.rgoarchitects.com/Files/fallacies.pdf] | ]]></ac:plain-text-body></ac:structured-macro> |
...
08. Locking (LCK) LCK10-J. Do not use incorrect forms of the double-checked locking idiom