samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.java
changeset 112 64308321f7bd
parent 111 3905a2e66b9b
     1.1 --- a/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.java	Sat Jun 14 09:54:36 2008 +0200
     1.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.java	Sat Jun 14 09:54:37 2008 +0200
     1.3 @@ -5,27 +5,34 @@
     1.4  public class CriticalSectionSynchronizedWithAssert<T extends Comparable<T>> implements CriticalSection<T> {
     1.5      private T pilot;
     1.6      private int cnt;
     1.7 -    private boolean working;
     1.8      
     1.9      public synchronized void assignPilot(T pilot) {
    1.10          assert !working : "Shall not be working yet in order to be consistent";
    1.11          this.pilot = pilot;
    1.12      }
    1.13  
    1.14 +    // BEGIN: reentrant.assert
    1.15 +    private boolean working;
    1.16      public int sumBigger(Collection<T> args) {
    1.17          assert !working : "Shall not be working yet in order to be consistent";
    1.18          working = true;
    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              working = false;
    1.29          }
    1.30      }
    1.31 +    // END: reentrant.assert
    1.32 +    
    1.33 +    
    1.34 +    private int doCriticalSection(Collection<T> args) {
    1.35 +        for (T cmp : args) {
    1.36 +            if (pilot.compareTo(cmp) < 0) {
    1.37 +                cnt++;
    1.38 +            }
    1.39 +        }
    1.40 +        return cnt;
    1.41 +    }
    1.42      
    1.43      public int getCount() {
    1.44          assert !working : "Shall not be working yet in order to be consistent";