TRS.CSFS
Avoid causing deadlock by calling a "synchronized" method from a "synchronized" method
Description
This rule flags code that calls a "synchronized" method from another "synchronized" method.
There are many scenarios for creating deadlocks; most fall into the following categories:
- A thread interdependency in which two or more threads are waiting on one another
- A thread that has an indefinite wait period (such as a blocking call) in which other threads depend on an object that this thread has locked
- A combination of the two.
Example
package TRS;
public class CSFS {
private synchronized void method1 () {
// do something
}
synchronized void method2 () {
method1 () // violation
}
}
Repair
Try not to synchronize the whole method; make a synchronized block that needs to be synchronized.
Reference
Daconta, M., Monk, E., Keller, J., Bohnenberger, K. Java Pitfalls. John Wiley & Sons, pp. 50 - 60.
|