samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrant.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:36 +0200
changeset 111 3905a2e66b9b
child 112 64308321f7bd
permissions -rw-r--r--
Sample code with various attempts to fight with reentrant code
jtulach@111
     1
package org.apidesign.reentrant;
jtulach@111
     2
jtulach@111
     3
import java.util.Collection;
jtulach@111
     4
import java.util.concurrent.atomic.AtomicInteger;
jtulach@111
     5
jtulach@111
     6
public class CriticalSectionReentrant<T extends Comparable<T>> implements CriticalSection<T> {
jtulach@111
     7
    private T pilot;
jtulach@111
     8
    private AtomicInteger cnt = new AtomicInteger();
jtulach@111
     9
    
jtulach@111
    10
    public void assignPilot(T pilot) {
jtulach@111
    11
        this.pilot = pilot;
jtulach@111
    12
    }
jtulach@111
    13
jtulach@111
    14
    public int sumBigger(Collection<T> args) {
jtulach@111
    15
        T pilotCopy = this.pilot;
jtulach@111
    16
        int own = 0;
jtulach@111
    17
        for (T cmp : args) {
jtulach@111
    18
            if (pilotCopy.compareTo(cmp) < 0) {
jtulach@111
    19
                own++;
jtulach@111
    20
            }
jtulach@111
    21
        }
jtulach@111
    22
        cnt.addAndGet(own);
jtulach@111
    23
        return own;
jtulach@111
    24
        
jtulach@111
    25
        
jtulach@111
    26
    }
jtulach@111
    27
jtulach@111
    28
    public int getCount() {
jtulach@111
    29
        return cnt.get();
jtulach@111
    30
    }
jtulach@111
    31
}