author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:54:36 +0200 | |
changeset 111 | 3905a2e66b9b |
child 112 | 64308321f7bd |
permissions | -rw-r--r-- |
jtulach@111 | 1 |
package org.apidesign.reentrant; |
jtulach@111 | 2 |
|
jtulach@111 | 3 |
import java.nio.channels.NonReadableChannelException; |
jtulach@111 | 4 |
import java.util.Collection; |
jtulach@111 | 5 |
import java.util.concurrent.locks.Lock; |
jtulach@111 | 6 |
|
jtulach@111 | 7 |
public class CriticalSectionSynchronizedWithNonReentrantLock<T extends Comparable<T>> implements CriticalSection<T> { |
jtulach@111 | 8 |
private T pilot; |
jtulach@111 | 9 |
private int cnt; |
jtulach@111 | 10 |
private Lock lock = new NonReentrantLock(); |
jtulach@111 | 11 |
|
jtulach@111 | 12 |
public void assignPilot(T pilot) { |
jtulach@111 | 13 |
lock.lock(); |
jtulach@111 | 14 |
try { |
jtulach@111 | 15 |
this.pilot = pilot; |
jtulach@111 | 16 |
} finally { |
jtulach@111 | 17 |
lock.unlock(); |
jtulach@111 | 18 |
} |
jtulach@111 | 19 |
} |
jtulach@111 | 20 |
|
jtulach@111 | 21 |
public int sumBigger(Collection<T> args) { |
jtulach@111 | 22 |
lock.lock(); |
jtulach@111 | 23 |
try { |
jtulach@111 | 24 |
for (T cmp : args) { |
jtulach@111 | 25 |
if (pilot.compareTo(cmp) < 0) { |
jtulach@111 | 26 |
cnt++; |
jtulach@111 | 27 |
} |
jtulach@111 | 28 |
} |
jtulach@111 | 29 |
return cnt; |
jtulach@111 | 30 |
} finally { |
jtulach@111 | 31 |
lock.unlock(); |
jtulach@111 | 32 |
} |
jtulach@111 | 33 |
} |
jtulach@111 | 34 |
|
jtulach@111 | 35 |
public int getCount() { |
jtulach@111 | 36 |
lock.lock(); |
jtulach@111 | 37 |
try { |
jtulach@111 | 38 |
return cnt; |
jtulach@111 | 39 |
} finally { |
jtulach@111 | 40 |
lock.unlock(); |
jtulach@111 | 41 |
} |
jtulach@111 | 42 |
} |
jtulach@111 | 43 |
} |