samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.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 
     5 public class CriticalSectionSynchronizedWithAssert<T extends Comparable<T>> implements CriticalSection<T> {
     6     private T pilot;
     7     private int cnt;
     8     private boolean working;
     9     
    10     public synchronized void assignPilot(T pilot) {
    11         assert !working : "Shall not be working yet in order to be consistent";
    12         this.pilot = pilot;
    13     }
    14 
    15     public int sumBigger(Collection<T> args) {
    16         assert !working : "Shall not be working yet in order to be consistent";
    17         working = true;
    18         try {
    19             for (T cmp : args) {
    20                 if (pilot.compareTo(cmp) < 0) {
    21                     cnt++;
    22                 }
    23             }
    24             return cnt;
    25         } finally {
    26             working = false;
    27         }
    28     }
    29     
    30     public int getCount() {
    31         assert !working : "Shall not be working yet in order to be consistent";
    32         return cnt;
    33     }
    34 }