7. Lock contention example public synchronized void setProperty(Object newValue) { // A value = newValue; notifyPropertyChange(); } private void notifyPropertyChange() { synchronized(listeners) {// B for (IListener listener : listeners) listener.changed(); // C } }
8. Lock contention example public void setProperty(Object newValue) { synchronized(this) {// A value = newValue; } notifyPropertyChange(); } private void notifyPropertyChange() { synchronized(listeners) {// B for (IListener listener : listeners) listener.changed(); // C } }
9. Lock contention example public void setProperty(Object newValue) { synchronized(this) {// A value = newValue; } notifyPropertyChange(); } private void notifyPropertyChange() { IListener[] tmp; synchronized(listeners) {// B tmp = listeners.clone(); } for (IListener listener : tmp) listener.changed(); // C }
10. Deadlock preventer Shortcut to launch application with lock monitor agent activated Detects Inconsistent locking order while acquiring locks between all threads in JVM Extracts lock statistics for tuning lock contention
11. Deadlock preventer - Automation Adding the Deadlock preventer is as simple as adding the following two arguments to the “java” executable: -javaagent:path-toom.freescale.deadlockPreventer.wrapper_1.0.0om.freescale.deadlockpreventer.jar -Xbootclasspath/a:path-toom.freescale.deadlockPreventer.wrapper_1.0.0om.freescale.deadlockpreventer.jar;path-toavassist.wrapper_1.0.0avassist.jar By default just prints all inconsistent locks to console output. There’s a server version listening for the deadlock preventer monitor events
12. References Author blog http://sergebeauchamp.blogspot.com/ GitHub https://github.com/sbeauchamp/Deadlock-Preventer