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
     1 package org.apidesign.reentrant;
     2 
     3 import java.util.Collection;
     4 import java.util.concurrent.atomic.AtomicInteger;
     5 
     6 public class CriticalSectionReentrant<T extends Comparable<T>> implements CriticalSection<T> {
     7     private T pilot;
     8     private AtomicInteger cnt = new AtomicInteger();
     9     
    10     public void assignPilot(T pilot) {
    11         this.pilot = pilot;
    12     }
    13 
    14     public int sumBigger(Collection<T> args) {
    15         T pilotCopy = this.pilot;
    16         int own = 0;
    17         for (T cmp : args) {
    18             if (pilotCopy.compareTo(cmp) < 0) {
    19                 own++;
    20             }
    21         }
    22         cnt.addAndGet(own);
    23         return own;
    24         
    25         
    26     }
    27 
    28     public int getCount() {
    29         return cnt.get();
    30     }
    31 }