author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:54:37 +0200 | |
changeset 112 | 64308321f7bd |
parent 111 | 3905a2e66b9b |
permissions | -rw-r--r-- |
jtulach@111 | 1 |
package org.apidesign.reentrant; |
jtulach@111 | 2 |
|
jtulach@111 | 3 |
import java.util.Collection; |
jtulach@111 | 4 |
|
jtulach@111 | 5 |
public class CriticalSectionSynchronizedWithAssert<T extends Comparable<T>> implements CriticalSection<T> { |
jtulach@111 | 6 |
private T pilot; |
jtulach@111 | 7 |
private int cnt; |
jtulach@111 | 8 |
|
jtulach@111 | 9 |
public synchronized void assignPilot(T pilot) { |
jtulach@111 | 10 |
assert !working : "Shall not be working yet in order to be consistent"; |
jtulach@111 | 11 |
this.pilot = pilot; |
jtulach@111 | 12 |
} |
jtulach@111 | 13 |
|
jtulach@112 | 14 |
// BEGIN: reentrant.assert |
jtulach@112 | 15 |
private boolean working; |
jtulach@111 | 16 |
public int sumBigger(Collection<T> args) { |
jtulach@111 | 17 |
assert !working : "Shall not be working yet in order to be consistent"; |
jtulach@111 | 18 |
working = true; |
jtulach@111 | 19 |
try { |
jtulach@112 | 20 |
return doCriticalSection(args); |
jtulach@111 | 21 |
} finally { |
jtulach@111 | 22 |
working = false; |
jtulach@111 | 23 |
} |
jtulach@111 | 24 |
} |
jtulach@112 | 25 |
// END: reentrant.assert |
jtulach@112 | 26 |
|
jtulach@112 | 27 |
|
jtulach@112 | 28 |
private int doCriticalSection(Collection<T> args) { |
jtulach@112 | 29 |
for (T cmp : args) { |
jtulach@112 | 30 |
if (pilot.compareTo(cmp) < 0) { |
jtulach@112 | 31 |
cnt++; |
jtulach@112 | 32 |
} |
jtulach@112 | 33 |
} |
jtulach@112 | 34 |
return cnt; |
jtulach@112 | 35 |
} |
jtulach@111 | 36 |
|
jtulach@111 | 37 |
public int getCount() { |
jtulach@111 | 38 |
assert !working : "Shall not be working yet in order to be consistent"; |
jtulach@111 | 39 |
return cnt; |
jtulach@111 | 40 |
} |
jtulach@111 | 41 |
} |