1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrant.java Sat Jun 14 09:54:36 2008 +0200
1.3 @@ -0,0 +1,31 @@
1.4 +package org.apidesign.reentrant;
1.5 +
1.6 +import java.util.Collection;
1.7 +import java.util.concurrent.atomic.AtomicInteger;
1.8 +
1.9 +public class CriticalSectionReentrant<T extends Comparable<T>> implements CriticalSection<T> {
1.10 + private T pilot;
1.11 + private AtomicInteger cnt = new AtomicInteger();
1.12 +
1.13 + public void assignPilot(T pilot) {
1.14 + this.pilot = pilot;
1.15 + }
1.16 +
1.17 + public int sumBigger(Collection<T> args) {
1.18 + T pilotCopy = this.pilot;
1.19 + int own = 0;
1.20 + for (T cmp : args) {
1.21 + if (pilotCopy.compareTo(cmp) < 0) {
1.22 + own++;
1.23 + }
1.24 + }
1.25 + cnt.addAndGet(own);
1.26 + return own;
1.27 +
1.28 +
1.29 + }
1.30 +
1.31 + public int getCount() {
1.32 + return cnt.get();
1.33 + }
1.34 +}