org.apache.catalina.tribes.transport.bio.util

Class SingleRemoveSynchronizedAddLock

public class SingleRemoveSynchronizedAddLock extends Object

The class SingleRemoveSynchronizedAddLock 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!

Version: 1.1

Author: Rainer Jung Peter Rossbach

Constructor Summary
SingleRemoveSynchronizedAddLock()
SingleRemoveSynchronizedAddLock(boolean dataAvailable)
Method Summary
voidabortRemove()
Abort any polling remover thread
longgetAddWaitTimeout()
longgetRemoveWaitTimeout()
booleanisAddLocked()
Check if an add thread owns the lock.
booleanisDataAvailable()
Check if the locked object has data available i.e. the remover can stop poling and get the lock.
booleanisRemoveLocked()
Check if the remove thread owns the lock.
booleanisRemovePolling()
Check if the remove thread is polling.
voidlockAdd()
Acquires the lock by an add thread and sets the add flag.
booleanlockRemove()
Acquires the lock by the remove thread and sets the remove flag.
voidsetAddWaitTimeout(long timeout)
Set value of addWaitTimeout
voidsetRemoveWaitTimeout(long timeout)
Set value of removeWaitTimeout
voidunlockAdd(boolean dataAvailable)
Releases the lock by an add thread and reset the remove flag.
voidunlockRemove()
Releases the lock by the remove thread and reset the add flag.

Constructor Detail

SingleRemoveSynchronizedAddLock

public SingleRemoveSynchronizedAddLock()

SingleRemoveSynchronizedAddLock

public SingleRemoveSynchronizedAddLock(boolean dataAvailable)

Method Detail

abortRemove

public void abortRemove()
Abort any polling remover thread

getAddWaitTimeout

public long getAddWaitTimeout()

Returns: Value of addWaitTimeout

getRemoveWaitTimeout

public long getRemoveWaitTimeout()

Returns: Value of removeWaitTimeout

isAddLocked

public boolean isAddLocked()
Check if an add thread owns the lock.

Returns: True iff an add thread owns the lock.

isDataAvailable

public boolean isDataAvailable()
Check if the locked object has data available i.e. the remover can stop poling and get the lock.

Returns: True iff the lock Object has data available.

isRemoveLocked

public boolean isRemoveLocked()
Check if the remove thread owns the lock.

Returns: True iff the remove thread owns the lock.

isRemovePolling

public boolean isRemovePolling()
Check if the remove thread is polling.

Returns: True iff the remove thread is polling.

lockAdd

public 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.

lockRemove

public 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.

setAddWaitTimeout

public void setAddWaitTimeout(long timeout)
Set value of addWaitTimeout

setRemoveWaitTimeout

public void setRemoveWaitTimeout(long timeout)
Set value of removeWaitTimeout

unlockAdd

public void unlockAdd(boolean dataAvailable)
Releases the lock by an add thread and reset the remove flag. If the reader thread is polling, notify it.

unlockRemove

public 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.
Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.