Holding locks while performing time consuming or blocking operations can severely degrade system performance and result in starvation. Furthermore, deadlock may result if too many interdependent threads block. Blocking operations include network I/O, invoking methods such as Console.readLine()
and letting a thread defer itself indefinitely.
Wiki Markup |
---|
If the Java Virtual Machine (JVM) interacts with a file system that operates over an unreliable network, file I/O might also incur a performance penalty. In such cases, avoid file I/O over the network while holding a lock. File operations such as logging that may block waiting for the output stream may be performed in a dedicated thread to speedup task processing. Logging requests can be added to a queue given that the {{put()}} operation incurs little overhead as compared to file I/O \[[Goetz 06, pg 244|AA. Java References#Goetz 06]\]. |
Noncompliant Code Example (intrinsic lock)
...