author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:54:41 +0200 | |
changeset 115 | 2c1b90108e02 |
parent 112 | 64308321f7bd |
child 205 | 6ad6f4162691 |
permissions | -rw-r--r-- |
jtulach@111 | 1 |
package org.apidesign.reentrant; |
jtulach@111 | 2 |
|
jtulach@111 | 3 |
import java.util.Collection; |
jtulach@111 | 4 |
import java.util.concurrent.atomic.AtomicInteger; |
jtulach@111 | 5 |
|
jtulach@111 | 6 |
public class CriticalSectionReentrant<T extends Comparable<T>> implements CriticalSection<T> { |
jtulach@111 | 7 |
private T pilot; |
jtulach@111 | 8 |
private AtomicInteger cnt = new AtomicInteger(); |
jtulach@111 | 9 |
|
jtulach@111 | 10 |
public void assignPilot(T pilot) { |
jtulach@111 | 11 |
this.pilot = pilot; |
jtulach@111 | 12 |
} |
jtulach@111 | 13 |
|
jtulach@115 | 14 |
// BEGIN: reentrant.merge.int |
jtulach@111 | 15 |
public int sumBigger(Collection<T> args) { |
jtulach@111 | 16 |
T pilotCopy = this.pilot; |
jtulach@112 | 17 |
int own = doCriticalSection(args, pilotCopy); |
jtulach@112 | 18 |
// now merge with global state |
jtulach@112 | 19 |
cnt.addAndGet(own); |
jtulach@112 | 20 |
return own; |
jtulach@112 | 21 |
} |
jtulach@115 | 22 |
// END: reentrant.merge.int |
jtulach@112 | 23 |
|
jtulach@112 | 24 |
private int doCriticalSection(Collection<T> args, T pilotCopy) { |
jtulach@111 | 25 |
int own = 0; |
jtulach@111 | 26 |
for (T cmp : args) { |
jtulach@111 | 27 |
if (pilotCopy.compareTo(cmp) < 0) { |
jtulach@111 | 28 |
own++; |
jtulach@111 | 29 |
} |
jtulach@111 | 30 |
} |
jtulach@111 | 31 |
return own; |
jtulach@111 | 32 |
} |
jtulach@111 | 33 |
|
jtulach@111 | 34 |
public int getCount() { |
jtulach@111 | 35 |
return cnt.get(); |
jtulach@111 | 36 |
} |
jtulach@111 | 37 |
} |