FileDocCategorySizeDatePackage
SingleRemoveSynchronizedAddLock.javaAPI DocApache Tomcat 6.0.147797Fri Jul 20 04:20:36 BST 2007org.apache.catalina.tribes.transport.bio.util

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!
author
Rainer Jung
author
Peter Rossbach
version
1.1

Fields Summary
private long
addWaitTimeout
Time 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
removeWaitTimeout
Time 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
remover
The 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
addLocked
A flag indicating, if an add thread owns the lock.
private boolean
removeLocked
A flag indicating, if the remove thread owns the lock.
private boolean
removeEnabled
A flag indicating, if the remove thread is allowed to wait for the lock. The flag is set to false, when aborting.
private boolean
dataAvailable
A flag indicating, if the remover needs polling. It indicates, if the locked object has data available to be removed.
Constructors Summary
public SingleRemoveSynchronizedAddLock()

    
public SingleRemoveSynchronizedAddLock(boolean dataAvailable)

        this.dataAvailable=dataAvailable;
    
Methods Summary
public synchronized voidabortRemove()
Abort any polling remover thread

        removeEnabled=false;
        if ( remover != null ) {
            remover.interrupt();
        }
    
public synchronized longgetAddWaitTimeout()

return
Value of addWaitTimeout


             
        
        return addWaitTimeout;
    
public synchronized longgetRemoveWaitTimeout()

return
Value of removeWaitTimeout

        return removeWaitTimeout;
    
public synchronized booleanisAddLocked()
Check if an add thread owns the lock.

return
True iff an add thread owns the lock.

        return addLocked;
    
public synchronized booleanisDataAvailable()
Check if the locked object has data available i.e. the remover can stop poling and get the lock.

return
True iff the lock Object has data available.

        return dataAvailable;
    
public synchronized booleanisRemoveLocked()
Check if the remove thread owns the lock.

return
True iff the remove thread owns the lock.

        return removeLocked;
    
public synchronized booleanisRemovePolling()
Check if the remove thread is polling.

return
True iff the remove thread is polling.

        if ( remover != null ) {
            return true;
        }
        return false;
    
public synchronized voidlockAdd()
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 booleanlockRemove()
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 voidsetAddWaitTimeout(long timeout)
Set value of addWaitTimeout

        addWaitTimeout = timeout;
    
public synchronized voidsetRemoveWaitTimeout(long timeout)
Set value of removeWaitTimeout

        removeWaitTimeout = timeout;
    
public synchronized voidunlockAdd(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 voidunlockRemove()
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();