# HG changeset patch # User Jaroslav Tulach # Date 1213430068 -7200 # Node ID e492694451f67a570ff912636158e06d50d15bce # Parent 461397695234cb7b75de355b6cab4e5e2d46739a Deadlocks on this and internal lock explained diff -r 461397695234 -r e492694451f6 samples/deadlock/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/build.xml Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,69 @@ + + + + + + Builds, tests, and runs the project deadlock. + + + diff -r 461397695234 -r e492694451f6 samples/deadlock/nbproject/build-impl.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/nbproject/build-impl.xml Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,627 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + diff -r 461397695234 -r e492694451f6 samples/deadlock/nbproject/genfiles.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/nbproject/genfiles.properties Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,8 @@ +build.xml.data.CRC32=f2e9f310 +build.xml.script.CRC32=b3c8dc02 +build.xml.stylesheet.CRC32=be360661 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=f2e9f310 +nbproject/build-impl.xml.script.CRC32=db044ecf +nbproject/build-impl.xml.stylesheet.CRC32=f1d9da08 diff -r 461397695234 -r e492694451f6 samples/deadlock/nbproject/project.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/nbproject/project.properties Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,63 @@ +application.title=deadlock +application.vendor=jarda +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/deadlock.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar +file.reference.org-netbeans-insane.jar=../libs/dist/org-netbeans-insane.jar +file.reference.org-netbeans-modules-nbjunit.jar=../libs/dist/org-netbeans-modules-nbjunit.jar +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${file.reference.junit-4.4.jar}:\ + ${file.reference.org-netbeans-modules-nbjunit.jar}:\ + ${file.reference.org-netbeans-insane.jar} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs=-ea +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff -r 461397695234 -r e492694451f6 samples/deadlock/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/nbproject/project.xml Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,16 @@ + + + org.netbeans.modules.java.j2seproject + + + deadlock + 1.6.5 + + + + + + + + + diff -r 461397695234 -r e492694451f6 samples/deadlock/src/org/apidesign/deadlock/SynchronizedFields.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/src/org/apidesign/deadlock/SynchronizedFields.java Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,26 @@ +package org.apidesign.deadlock; + +public final class SynchronizedFields { + // BEGIN: deadlock.holdsLock + private int counter; + + private int getCounter() { + assert Thread.holdsLock(this); + return counter; + } + + private void setCounter(int c) { + assert Thread.holdsLock(this); + counter = c; + } + // END: deadlock.holdsLock + + + public synchronized void increment() { + setCounter(getCounter() + 1); + } + + public void unsafeDecrement() { + setCounter(getCounter() - 1); + } +} diff -r 461397695234 -r e492694451f6 samples/deadlock/src/org/apidesign/deadlock/SynchronizedFieldsInternally.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/src/org/apidesign/deadlock/SynchronizedFieldsInternally.java Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,29 @@ +package org.apidesign.deadlock; + +public final class SynchronizedFieldsInternally { + // BEGIN: deadlock.ownLock + private final Object LOCK = new Object(); + private int counter; + + private int getCounter() { + assert Thread.holdsLock(LOCK); + return counter; + } + + private void setCounter(int c) { + assert Thread.holdsLock(LOCK); + counter = c; + } + // END: deadlock.ownLock + + + public void increment() { + synchronized (LOCK) { + setCounter(getCounter() + 1); + } + } + + public void unsafeDecrement() { + setCounter(getCounter() - 1); + } +} diff -r 461397695234 -r e492694451f6 samples/deadlock/test/org/apidesign/deadlock/SynchronizedFieldsInternallyTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/test/org/apidesign/deadlock/SynchronizedFieldsInternallyTest.java Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,46 @@ +package org.apidesign.deadlock; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class SynchronizedFieldsInternallyTest { + public SynchronizedFieldsInternallyTest() { + } + + @Test + public void increment() { + SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally(); + instance.increment(); + } + + @Test + public void unsafeDecrement() { + SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally(); + try { + instance.unsafeDecrement(); + } catch (java.lang.AssertionError ex) { + // OK + return; + } + fail("This will fail as unsafeDecrement is not synchronized, and that" + + "is why it cannot access the field using getter and setter" + ); + } + + @Test + public void fixUnsafeDecrementFromOutside() { + SynchronizedFieldsInternally instance = new SynchronizedFieldsInternally(); + try { + synchronized (instance) { + instance.unsafeDecrement(); + } + } catch (AssertionError ex) { + // OK + return; + } + fail("Unlike the SynchronizedFieldsTest, the fix by wrapping instance" + + "into own synchronized block will not help, neither any other" + + "fix, the lock is really private" + ); + } +} \ No newline at end of file diff -r 461397695234 -r e492694451f6 samples/deadlock/test/org/apidesign/deadlock/SynchronizedFieldsTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/deadlock/test/org/apidesign/deadlock/SynchronizedFieldsTest.java Sat Jun 14 09:54:28 2008 +0200 @@ -0,0 +1,39 @@ +package org.apidesign.deadlock; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class SynchronizedFieldsTest { + public SynchronizedFieldsTest() { + } + + @Test + public void increment() { + SynchronizedFields instance = new SynchronizedFields(); + instance.increment(); + } + + @Test + public void unsafeDecrement() { + SynchronizedFields instance = new SynchronizedFields(); + try { + instance.unsafeDecrement(); + } catch (java.lang.AssertionError ex) { + // OK + return; + } + fail("This will fail as unsafeDecrement is not synchronized, and that" + + "is why it cannot access the field using getter and setter" + ); + } + + @Test + public void fixUnsafeDecrementFromOutside() { + SynchronizedFields instance = new SynchronizedFields(); + synchronized (instance) { + // in contract to original "Monitors", one can "fix" this + // problem from outside by using synchronizing externally + instance.unsafeDecrement(); + } + } +} \ No newline at end of file