samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantForLoop.java
1 package org.apidesign.reentrant;
3 import java.util.Collection;
4 import java.util.concurrent.atomic.AtomicInteger;
6 public class CriticalSectionReentrantForLoop<T extends Comparable<T>> implements CriticalSection<T> {
7 private volatile T pilot;
8 private AtomicInteger cnt = new AtomicInteger();
10 public void assignPilot(T pilot) {
14 // BEGIN: reentrant.merge.for
15 public int sumBigger(Collection<T> args) {
16 T pilotCopy = this.pilot;
18 int previous = cnt.get();
19 int own = doCriticalSection(args, pilotCopy);
20 // if there was no parallel or reentrant change,
21 // apply and return. Otherwise try once more.
22 if (cnt.compareAndSet(previous, own + previous)) {
27 // END: reentrant.merge.for
29 private int doCriticalSection(Collection<T> args, T pilotCopy) {
32 if (pilotCopy.compareTo(cmp) < 0) {
39 public int getCount() {