Fields Summary |
---|
private long | addWaitTimeoutTime in milliseconds after which threads
waiting for an add lock are woken up.
This is used as a safety measure in case
thread notification via the unlock methods
has a bug. |
private long | removeWaitTimeoutTime in milliseconds after which threads
waiting for a remove lock are woken up.
This is used as a safety measure in case
thread notification via the unlock methods
has a bug. |
private Thread | removerThe current remove thread.
It is set to the remove thread polling for entries.
It is reset to null when the remove thread
releases the lock and proceeds processing
the removed entries. |
private boolean | addLockedA flag indicating, if an add thread owns the lock. |
private boolean | removeLockedA flag indicating, if the remove thread owns the lock. |
private boolean | removeEnabledA flag indicating, if the remove thread is allowed
to wait for the lock. The flag is set to false, when aborting. |
private boolean | dataAvailableA flag indicating, if the remover needs polling.
It indicates, if the locked object has data available
to be removed. |
Methods Summary |
---|
public synchronized void | abortRemove()Abort any polling remover thread
removeEnabled=false;
if ( remover != null ) {
remover.interrupt();
}
|
public synchronized long | getAddWaitTimeout()
return addWaitTimeout;
|
public synchronized long | getRemoveWaitTimeout()
return removeWaitTimeout;
|
public synchronized boolean | isAddLocked()Check if an add thread owns the lock.
return addLocked;
|
public synchronized boolean | isDataAvailable()Check if the locked object has data available
i.e. the remover can stop poling and get the lock.
return dataAvailable;
|
public synchronized boolean | isRemoveLocked()Check if the remove thread owns the lock.
return removeLocked;
|
public synchronized boolean | isRemovePolling()Check if the remove thread is polling.
if ( remover != null ) {
return true;
}
return false;
|
public synchronized void | lockAdd()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.
if ( addLocked || removeLocked ) {
do {
try {
wait(addWaitTimeout);
} catch ( InterruptedException e ) {
Thread.currentThread().interrupted();
}
} while ( addLocked || removeLocked );
}
addLocked=true;
|
public synchronized boolean | lockRemove()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.
removeLocked=false;
removeEnabled=true;
if ( ( addLocked || ! dataAvailable ) && removeEnabled ) {
remover=Thread.currentThread();
do {
try {
wait(removeWaitTimeout);
} catch ( InterruptedException e ) {
Thread.currentThread().interrupted();
}
} while ( ( addLocked || ! dataAvailable ) && removeEnabled );
remover=null;
}
if ( removeEnabled ) {
removeLocked=true;
}
return removeLocked;
|
public synchronized void | setAddWaitTimeout(long timeout)Set value of addWaitTimeout
addWaitTimeout = timeout;
|
public synchronized void | setRemoveWaitTimeout(long timeout)Set value of removeWaitTimeout
removeWaitTimeout = timeout;
|
public synchronized void | unlockAdd(boolean dataAvailable)Releases the lock by an add thread and reset the remove flag.
If the reader thread is polling, notify it.
addLocked=false;
this.dataAvailable=dataAvailable;
if ( ( remover != null ) && ( dataAvailable || ! removeEnabled ) ) {
remover.interrupt();
} else {
notifyAll();
}
|
public synchronized void | unlockRemove()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.
removeLocked=false;
dataAvailable=false;
notifyAll();
|