To avoid data corruption in multithreaded Java programs, shared data must be protected from concurrent modifications and accesses. Locking can be performed at the object level using synchronized methods, synchronized blocks, or the java.util.concurrent
dynamic lock objects. However, excessive use of locking can result in deadlocks.
Java neither prevents deadlocks nor requires their detection \ [[JLS 2005|AA. References#JLS 05]\]. Deadlock can occur when two or more threads request and release locks in different orders. Consequently, programs are required to avoid deadlock by acquiring and releasing locks in the same order. Wiki Markup
Additionally, synchronization should be limited to cases where it is absolutely necessary. For example, the paint()
, dispose()
, stop()
, and destroy()
methods should never be synchronized in an applet because they are always called and used from dedicated threads. Furthermore, the Thread.stop()
and Thread.destroy()
methods are deprecated; see rule THI05-J. Do not use Thread.stop() to terminate threads for more information.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
LCK07-J | low | likely | high | P3 | L3 |
Automated Detection
Some static analysis tools can detect violations of this rule.
...
CWE-833. Deadlock |
Bibliography
[JLS 2005] | |||||
| |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="794ea7a4-cff3-42e7-b1f2-e207c2b22f02"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. References#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="0358fbfc-92af-4af7-9c67-94920ca0a5b1"><ac:plain-text-body><![CDATA[ | [[Halloway 2000 | AA. References#Halloway 00]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...