1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/SynchronizedFieldsInternally.java Sat Jun 14 09:54:28 2008 +0200
1.3 @@ -0,0 +1,29 @@
1.4 +package org.apidesign.deadlock;
1.5 +
1.6 +public final class SynchronizedFieldsInternally {
1.7 + // BEGIN: deadlock.ownLock
1.8 + private final Object LOCK = new Object();
1.9 + private int counter;
1.10 +
1.11 + private int getCounter() {
1.12 + assert Thread.holdsLock(LOCK);
1.13 + return counter;
1.14 + }
1.15 +
1.16 + private void setCounter(int c) {
1.17 + assert Thread.holdsLock(LOCK);
1.18 + counter = c;
1.19 + }
1.20 + // END: deadlock.ownLock
1.21 +
1.22 +
1.23 + public void increment() {
1.24 + synchronized (LOCK) {
1.25 + setCounter(getCounter() + 1);
1.26 + }
1.27 + }
1.28 +
1.29 + public void unsafeDecrement() {
1.30 + setCounter(getCounter() - 1);
1.31 + }
1.32 +}