samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithNonReentrantLock.java
changeset 112 64308321f7bd
parent 111 3905a2e66b9b
     1.1 --- a/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithNonReentrantLock.java	Sat Jun 14 09:54:36 2008 +0200
     1.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithNonReentrantLock.java	Sat Jun 14 09:54:37 2008 +0200
     1.3 @@ -7,7 +7,6 @@
     1.4  public class CriticalSectionSynchronizedWithNonReentrantLock<T extends Comparable<T>> implements CriticalSection<T> {
     1.5      private T pilot;
     1.6      private int cnt;
     1.7 -    private Lock lock = new NonReentrantLock();
     1.8      
     1.9      public void assignPilot(T pilot) {
    1.10          lock.lock();
    1.11 @@ -18,19 +17,26 @@
    1.12          }
    1.13      }
    1.14  
    1.15 +    // BEGIN: reentrant.nonreentrant.lock
    1.16 +    private Lock lock = new NonReentrantLock();
    1.17      public int sumBigger(Collection<T> args) {
    1.18          lock.lock();
    1.19          try {
    1.20 -            for (T cmp : args) {
    1.21 -                if (pilot.compareTo(cmp) < 0) {
    1.22 -                    cnt++;
    1.23 -                }
    1.24 -            }
    1.25 -            return cnt;
    1.26 +            return doCriticalSection(args);
    1.27          } finally {
    1.28              lock.unlock();
    1.29          }
    1.30      }
    1.31 +    // END: reentrant.nonreentrant.lock
    1.32 +    
    1.33 +    private int doCriticalSection(Collection<T> args) {
    1.34 +        for (T cmp : args) {
    1.35 +            if (pilot.compareTo(cmp) < 0) {
    1.36 +                cnt++;
    1.37 +            }
    1.38 +        }
    1.39 +        return cnt;
    1.40 +    }
    1.41      
    1.42      public int getCount() {
    1.43          lock.lock();