author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:54:37 +0200 | |
changeset 112 | 64308321f7bd |
parent 111 | 3905a2e66b9b |
child 115 | 2c1b90108e02 |
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@111 | 14 |
public int sumBigger(Collection<T> args) { |
jtulach@111 | 15 |
T pilotCopy = this.pilot; |
jtulach@112 | 16 |
int own = doCriticalSection(args, pilotCopy); |
jtulach@112 | 17 |
// now merge with global state |
jtulach@112 | 18 |
cnt.addAndGet(own); |
jtulach@112 | 19 |
return own; |
jtulach@112 | 20 |
} |
jtulach@112 | 21 |
|
jtulach@112 | 22 |
private int doCriticalSection(Collection<T> args, T pilotCopy) { |
jtulach@111 | 23 |
int own = 0; |
jtulach@111 | 24 |
for (T cmp : args) { |
jtulach@111 | 25 |
if (pilotCopy.compareTo(cmp) < 0) { |
jtulach@111 | 26 |
own++; |
jtulach@111 | 27 |
} |
jtulach@111 | 28 |
} |
jtulach@111 | 29 |
return own; |
jtulach@111 | 30 |
} |
jtulach@111 | 31 |
|
jtulach@111 | 32 |
public int getCount() { |
jtulach@111 | 33 |
return cnt.get(); |
jtulach@111 | 34 |
} |
jtulach@111 | 35 |
} |