author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 10:04:53 +0200 | |
changeset 210 | acf2c31e22d4 |
parent 209 | 1c999569643b |
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 |
|
jtulach@111 | 9 |
public void assignPilot(T pilot) { |
jtulach@111 | 10 |
this.pilot = pilot; |
jtulach@111 | 11 |
} |
jtulach@111 | 12 |
|
jtulach@115 | 13 |
// BEGIN: reentrant.merge.int |
jtulach@210 | 14 |
private AtomicInteger cnt = new AtomicInteger(); |
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 |
} |