...
Wiki Markup |
---|
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 whenwhile 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 {{put()}} operation incurs little overhead as compared to file I/O \[[Goetz 2006|AA. Bibliography#Goetz 06]\]. |
...
Code Block | ||
---|---|---|
| ||
public synchronized void doSomething(long time)
throws InterruptedException {
// ...
Thread.sleep(time);
}
|
...
Code Block | ||
---|---|---|
| ||
public synchronized void doSomething(long timeout) throws InterruptedException { // ... while (<condition does not hold>) { wait(timeout); // Immediately releases the current monitor } } |
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.
...
This noncompliant code example shows defines a sendPage()
method that sends a Page
object from a server to a client. The method is synchronized to protect the pageBuff
array when multiple threads request concurrent access.
...
In this compliant solution, the synchronized getPageunsynchronized sendPage()
method is called from an unsynchronized sendPagecalls the synchronized getPage()
method to retrieve the requested Page
in the pageBuff
array. After the Page
is retrieved, sendPage()
calls the unsynchronized deliverPage()
method to deliver the Page
to the client.
Code Block | ||
---|---|---|
| ||
// No synchronization public boolean sendPage(Socket socket, String pageName) { Page targetPage = getPage(pageName); if (targetPage == null){ return false; } return deliverPage(socket, targetPage); } // Requires synchronization private synchronized Page getPage(String pageName) { Page targetPage = null; for (Page p : pageBuff) { if (p.getName().equals(pageName)) { targetPage = p; } } return targetPage; } // Return false if an error occurs, true if successful public boolean deliverPage(Socket socket, Page page) { ObjectOutputStream out = null; boolean result = true; try { // Get the output stream to write the Page to out = new ObjectOutputStream(socket.getOutputStream()); // Send the page to the client out.writeObject(page);out.flush(); } catch (IOException io) { result = false; } finally { if (out != null) { try { out.flush(); out.close(); } catch (IOException e) { result = false; } } } return result; } |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b1e18c0448b5c2f0-20fd6f98-424847e0-a00f90dc-992c9ea0a0c0bdc818fedfe3"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#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="07b1fcbd44d7c94c-61d7b3c9-4abd439b-98829ff8-33da3474c00596c14a72ec47"><ac:plain-text-body><![CDATA[ | [[Grosso 2001 | AA. Bibliography#Grosso 01]] | [Chapter 10, Serialization | http://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="8ba4561426c5b2be-5b81b7e0-4a9148b5-9384ad46-d8eb766ec0efcaaf572cc1bf"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 17, Threads and Locks | 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="4acfb821ba25bb80-a13abbe7-46f649b2-ad3ea833-f7a691bf33a76027101d7b61"><ac:plain-text-body><![CDATA[ | [[Rotem 2008 | AA. Bibliography#Rotem 08]] | [Fallacies of Distributed Computing Explained | http://www.rgoarchitects.com/Files/fallacies.pdf] | ]]></ac:plain-text-body></ac:structured-macro> |
...