samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:37 +0200
changeset 112 64308321f7bd
parent 111 3905a2e66b9b
permissions -rw-r--r--
Reentrant access examples rewritten to make a bit more sence
     1 package org.apidesign.reentrant;
     2 
     3 import java.util.Collection;
     4 
     5 public class CriticalSectionSynchronizedWithAssert<T extends Comparable<T>> implements CriticalSection<T> {
     6     private T pilot;
     7     private int cnt;
     8     
     9     public synchronized void assignPilot(T pilot) {
    10         assert !working : "Shall not be working yet in order to be consistent";
    11         this.pilot = pilot;
    12     }
    13 
    14     // BEGIN: reentrant.assert
    15     private boolean working;
    16     public int sumBigger(Collection<T> args) {
    17         assert !working : "Shall not be working yet in order to be consistent";
    18         working = true;
    19         try {
    20             return doCriticalSection(args);
    21         } finally {
    22             working = false;
    23         }
    24     }
    25     // END: reentrant.assert
    26     
    27     
    28     private int doCriticalSection(Collection<T> args) {
    29         for (T cmp : args) {
    30             if (pilot.compareTo(cmp) < 0) {
    31                 cnt++;
    32             }
    33         }
    34         return cnt;
    35     }
    36     
    37     public int getCount() {
    38         assert !working : "Shall not be working yet in order to be consistent";
    39         return cnt;
    40     }
    41 }