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 indefinitely. Blocking operations include network, file and console I/O (for instanceexample, invoking a method such as Console.readLine()
), and object serialization. Deferring a thread indefinitely also constitutes a blocking operation.
Wiki Markup |
---|
If the Java Virtual Machine (JVM) interacts with a file system that operates over an unreliable network, file I/O might incur a performance penalty. In such cases, avoid file I/O over the network whilewhen holding a lock. File operations (such as logging) that may block waiting for the output stream lock or for I/O to complete may be performed in a dedicated thread to speed up 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 (
...
deferring a thread)
This noncompliant code example defines a utility method that accepts a time
parameter.
Code Block | ||
---|---|---|
| ||
privatepublic synchronized void doSomething(long time) throws InterruptedException { // ... Thread.sleep(time); } |
...
This compliant solution defines the doSomething()
method with a timeout
parameter instead of the time
value. The use of the Object.wait()
method instead of Thread.sleep()
allows setting a time out for a period during which a notify signal may awaken the thread.
Code Block | ||
---|---|---|
| ||
privatepublic synchronized void doSomething(long timeout) throws InterruptedException { while (<condition does not hold>) { wait(timeout); // Immediately leaves current monitor } } |
...
Wiki Markup |
---|
According to the Java API class {{Object}} documentation \[[API 06|AA. Java References#API 06]\], class {{Object}} documentation: |
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.
...
Code Block | ||
---|---|---|
| ||
// Class Page is defined separately. It stores and returns the Page name via getName() public final boolean SUCCESS = true; public final boolean FAILURE = false; Page[] pageBuff = new Page[MAX_PAGE_SIZE]; public synchronized boolean sendPage(Socket socket, String pageName) throws IOException { // Get the output stream to write the Page to ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); // Find the Page requested by the client (this operation requires synchronization) Page targetPage = null; for(Page p : pageBuff) { if(p.getName().compareTo(pageName) == 0) { targetPage = p; } } // Requested Page does not exist if(targetPage == null) { return FAILUREfalse; } // Send the Page to the client (does not require any synchronization) out.writeObject(targetPage); out.flush(); out.close(); return SUCCESStrue; } |
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.
...
Code Block | ||
---|---|---|
| ||
public boolean sendPage(Socket socket, String pageName) { // No synchronization Page targetPage = getPage(pageName); if(targetPage == null) return FAILUREfalse; return deliverPage(socket, targetPage); } private synchronized Page getPage(String pageName) { // Requires synchronization Page targetPage = null; for(Page p : pageBuff) { if(p.getName().equals(pageName)) { targetPage = p; } } return targetPage; } public boolean deliverPage(Socket socket, Page page){ try{ // Get the output stream to write the Page to ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); // Send the Page to the client out.writeObject(page); } catch(IOException io){ // If recovery is not possible return FAILUREfalse return FAILUREfalse; } finally { out.flush(); out.close(); } return SUCCESStrue; } |
Exceptions
EX1: Classes that are compliant with the guideline CON24-J. Ensure that threads and tasks performing blocking operations can be terminated in that they provide an appropriate termination mechanism to callers, are allowed to violate this guideline.
...