Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added normative text. tweaked some grammar for better exposition

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 are forbidden to perform blocking operations while holding a lock.

Wiki Markup
If 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 when 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, givenassuming that the queue's {{put()}} operation incurs little overhead as compared to file I/O \[[Goetz 2006|AA. Bibliography#Goetz 06]\].

...

Because the method is synchronized, if when the thread is suspended, other threads are unable to use the synchronized methods of the class. The current object's monitor is not released continues to be held because the Thread.sleep() method does not have any lacks synchronization semantics, as detailed in rule THI00-J. Do not assume that the sleep(), yield() or getState() methods provide synchronization semantics.

...

This compliant solution defines the doSomething() method with a timeout parameter instead of rather than the time value. Using Object.wait() instead of Thread.sleep() allows setting a time out period during which a notification may awaken the thread.

...

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.

Ensure that a thread that holds Programs must ensure that threads that hold locks on other objects releases them 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 THI04-J. Notify all waiting threads instead of a single thread.

...

This noncompliant code example shows the method a sendPage() method that sends a Page object from a server to a client. The method is synchronized so that to protect the pageBuff array is accessed safely when multiple threads request concurrent access.

...

  1. Perform actions on data structures requiring synchronization.
  2. Create copies of the objects to be sent.
  3. Perform network calls in a separate unsynchronized method that does not require any synchronization.

In this compliant solution, the synchronized getPage() method is called from an unsynchronized sendPage() 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.

...

LCK09-EX1: Classes that provide an appropriate termination mechanism to callers are allowed permitted to violate this rule. See rule THI06-J. Ensure that threads performing blocking operations can be terminated.

LCK09-EX2: A method Method that requires 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 to avoid deadlock. See rule LCK07-J. Avoid deadlock by requesting and releasing locks in the same order for more information.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d822a01343dde6ac-ec1053fb-43594fb3-b5b0b157-f97c9ccbe8d93c894f80e15e"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

Class Object

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="19a3977092438cb2-c8f9f704-40f342b1-9b6cb895-071c87ce2a8dca70bf1ea812"><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="6258a405b9e7b80e-5f3d89ff-45ca4c5b-b5d4b53b-8a03506647165e472acd7ac3"><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="5fd52a5a413eed93-08ad0d2b-4e66416b-a307b049-0a0110b9dbcbd5073a459ca9"><ac:plain-text-body><![CDATA[

[[Rotem 2008

AA. Bibliography#Rotem 08]]

[Falacies of Distributed Computing Explained

http://www.rgoarchitects.com/Files/fallacies.pdf]

]]></ac:plain-text-body></ac:structured-macro>

...