samples/deadlock/src/org/apidesign/deadlock/SynchronizedFields.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:28 +0200
changeset 103 e492694451f6
permissions -rw-r--r--
Deadlocks on this and internal lock explained
jtulach@103
     1
package org.apidesign.deadlock;
jtulach@103
     2
jtulach@103
     3
public final class SynchronizedFields {
jtulach@103
     4
    // BEGIN: deadlock.holdsLock
jtulach@103
     5
    private int counter;
jtulach@103
     6
    
jtulach@103
     7
    private int getCounter() {
jtulach@103
     8
        assert Thread.holdsLock(this);
jtulach@103
     9
        return counter;
jtulach@103
    10
    } 
jtulach@103
    11
    
jtulach@103
    12
    private void setCounter(int c) {
jtulach@103
    13
        assert Thread.holdsLock(this);
jtulach@103
    14
        counter = c;
jtulach@103
    15
    }
jtulach@103
    16
    // END: deadlock.holdsLock
jtulach@103
    17
    
jtulach@103
    18
    
jtulach@103
    19
    public synchronized void increment() {
jtulach@103
    20
        setCounter(getCounter() + 1);
jtulach@103
    21
    }
jtulach@103
    22
    
jtulach@103
    23
    public void unsafeDecrement() {
jtulach@103
    24
        setCounter(getCounter() - 1);
jtulach@103
    25
    }
jtulach@103
    26
}