samples/reentrant/test/org/apidesign/reentrant/CriticalSectionBase.java
changeset 112 64308321f7bd
parent 111 3905a2e66b9b
child 263 7e8e995065c5
     1.1 --- a/samples/reentrant/test/org/apidesign/reentrant/CriticalSectionBase.java	Sat Jun 14 09:54:36 2008 +0200
     1.2 +++ b/samples/reentrant/test/org/apidesign/reentrant/CriticalSectionBase.java	Sat Jun 14 09:54:37 2008 +0200
     1.3 @@ -8,12 +8,17 @@
     1.4  
     1.5  public abstract class CriticalSectionBase {
     1.6      protected abstract CriticalSection<Integer> create();
     1.7 +    protected boolean reentrantJustOnce() {
     1.8 +        return false;
     1.9 +    }
    1.10  
    1.11 +    // BEGIN: reentrant.ok.call
    1.12      @Test
    1.13      public void testCriticalSectionWith15() {
    1.14          final CriticalSection<Integer> cs = create();
    1.15          testFor15(cs);
    1.16      }
    1.17 +    // END: reentrant.ok.call
    1.18      
    1.19      final void testFor15(CriticalSection<Integer> cs) {
    1.20          cs.assignPilot(15);
    1.21 @@ -41,9 +46,13 @@
    1.22          cs.assignPilot(10);
    1.23          
    1.24          class ChangePilotTo15 implements Runnable {
    1.25 +            // BEGIN: reentrant.forbidden.call
    1.26 +            // if this runnable is called from inside the critical section,
    1.27 +            // and the locks are non-reentrant then it throws an exception
    1.28              public void run() {
    1.29                  testFor15(cs);
    1.30              }
    1.31 +            // END: reentrant.forbidden.call
    1.32          }
    1.33          
    1.34          List<Integer> ints = new MyCollection(new ChangePilotTo15(), 3);
    1.35 @@ -64,7 +73,7 @@
    1.36      }
    1.37  
    1.38      class MyCollection extends ArrayList<Integer> {
    1.39 -        private final Runnable callback;
    1.40 +        private Runnable callback;
    1.41          private final int callbackBeforeIndex;
    1.42  
    1.43          public MyCollection(Runnable callback, int callbackAtIndex) {
    1.44 @@ -84,7 +93,12 @@
    1.45  
    1.46                  public Integer next() {
    1.47                      if (index++ == callbackBeforeIndex) {
    1.48 -                        callback.run();
    1.49 +                        if (callback != null) {
    1.50 +                            callback.run();
    1.51 +                        }
    1.52 +                        if (reentrantJustOnce()) {
    1.53 +                            callback = null;
    1.54 +                        }
    1.55                      }
    1.56                      
    1.57                      return delegate.next();