implement locking for accessing the queue
by a single remove thread and multiple add threads.
A thread is only allowed to be either the remove or
an add thread.
The lock can either be owned by the remove thread
or by a single add thread.
If the remove thread tries to get the lock,
but the queue is empty, it will block (poll)
until an add threads adds an entry to the queue and
releases the lock.
If the remove thread and add threads compete for
the lock and an add thread releases the lock, then
the remove thread will get the lock first.
The remove thread removes all entries in the queue
at once and proceeses them without further
polling the queue.
The lock is not reentrant, in the sense, that all
threads must release an owned lock before competing
for the lock again!
| Method from org.apache.catalina.cluster.util.SingleRemoveSynchronizedAddLock Detail: |
public synchronized void abortRemove() {
removeEnabled=false;
if ( remover != null ) {
remover.interrupt();
}
}
Abort any polling remover thread |
public synchronized long getAddWaitTimeout() {
return addWaitTimeout;
}
|
public synchronized long getRemoveWaitTimeout() {
return removeWaitTimeout;
}
|
public synchronized boolean isAddLocked() {
return addLocked;
}
Check if an add thread owns the lock. |
public synchronized boolean isDataAvailable() {
return dataAvailable;
}
Check if the locked object has data available
i.e. the remover can stop poling and get the lock. |
public synchronized boolean isRemoveLocked() {
return removeLocked;
}
Check if the remove thread owns the lock. |
public synchronized boolean isRemovePolling() {
if ( remover != null ) {
return true;
}
return false;
}
Check if the remove thread is polling. |
public synchronized void lockAdd() {
if ( addLocked || removeLocked ) {
do {
try {
wait(addWaitTimeout);
} catch ( InterruptedException e ) {
}
} while ( addLocked || removeLocked );
}
addLocked=true;
}
Acquires the lock by an add thread and sets the add flag.
If any add thread or the remove thread already acquired the lock
this add thread will block until the lock is released. |
public synchronized boolean lockRemove() {
removeLocked=false;
removeEnabled=true;
if ( ( addLocked || ! dataAvailable ) && removeEnabled ) {
remover=Thread.currentThread();
do {
try {
wait(removeWaitTimeout);
} catch ( InterruptedException e ) {
}
} while ( ( addLocked || ! dataAvailable ) && removeEnabled );
remover=null;
}
if ( removeEnabled ) {
removeLocked=true;
}
return removeLocked;
}
Acquires the lock by the remove thread and sets the remove flag.
If any add thread already acquired the lock or the queue is
empty, the remove thread will block until the lock is released
and the queue is not empty. |
public synchronized void setAddWaitTimeout(long timeout) {
addWaitTimeout = timeout;
}
Set value of addWaitTimeout |
public synchronized void setRemoveWaitTimeout(long timeout) {
removeWaitTimeout = timeout;
}
Set value of removeWaitTimeout |
public synchronized void unlockAdd(boolean dataAvailable) {
addLocked=false;
this.dataAvailable=dataAvailable;
if ( ( remover != null ) && ( dataAvailable || ! removeEnabled ) ) {
remover.interrupt();
} else {
notifyAll();
}
}
Releases the lock by an add thread and reset the remove flag.
If the reader thread is polling, notify it. |
public synchronized void unlockRemove() {
removeLocked=false;
dataAvailable=false;
notifyAll();
}
Releases the lock by the remove thread and reset the add flag.
Notify all waiting add threads,
that the lock has been released by the remove thread. |