FileDocCategorySizeDatePackage
BlockingQueue.javaAPI DocGlassfish v2 API10405Fri May 04 22:32:10 BST 2007com.sun.enterprise.util.collection

BlockingQueue

public class BlockingQueue extends Object
A BlockingQueue is a queue where remove() blocks if the queue is empty. The thread calling remove() blocks if the queue is empty while the add() notifies any waiting thread.

Fields Summary
static Logger
_logger
private boolean
closed
private boolean
aborted
private int
limit
private LinkedList
list
private int
waiters
Constructors Summary
public BlockingQueue()
Create a BlockingQueue that has an infinite queuelength with the specified timeout.

param
The maximum time remove() will block.
see
remove()

 // added for 4682740
	
	          	          	 
	  
		this(Integer.MAX_VALUE);
	
public BlockingQueue(int queueLimit)
Create a BlockingQueue that has the specified queue limit with the specified timeout.

param
The maximum time remove() will block.
param
The queue length after which TooManyTasksException is thrown.
see
remove()

		this.limit = queueLimit;
		this.list = new LinkedList();
                // START OF IASRI 4682740
                com.sun.enterprise.util.MonitorTask.addORBMonitorable(this);
                // END OF IASRI 4682740
	
Methods Summary
public voidabort()

		this.closed = this.aborted = true;
		synchronized (list) {
			list.notifyAll();
		}
	
public voidadd(int index, java.lang.Object object)
Add the job at the specified position. Probably based on priority?

		if (closed)
			throw new QueueClosedException("Queue closed.");
		synchronized (list) {
		    if (list.size() >= limit) {
			    throw new TooManyTasksException("Too many tasks in queue...");
		    }
		    list.add(index, object);
		    list.notify();
		}
	
public voidaddAll(java.util.Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

		if (closed)
			throw new QueueClosedException("Queue closed.");
		synchronized (list) {
		    if (list.size() >= limit) {
			    throw new TooManyTasksException("Too many tasks in queue...");
		    }
		    list.addAll(c);
		    list.notify();
		}
	
public voidaddFirst(java.lang.Object object)
Add to the head of the queue. Probably a high priority job?

		if (closed)
			throw new QueueClosedException("Queue closed.");
		synchronized (list) {
		    if (list.size() >= limit) {
			    throw new TooManyTasksException("Too many tasks in queue...");
		    }
		    list.addFirst(object);
		    list.notify();
		}
	
public voidaddLast(java.lang.Object object)
Add to the tail of the queue.

		if (closed)
			throw new QueueClosedException("Queue closed.");
		synchronized (list) {
		    if (list.size() >= limit) {
			    throw new TooManyTasksException("Too many tasks in queue...");
		    }
		    list.add(object);
		    list.notify();
		}
	
public intgetUnsyncSize()
Return the size of the queue, unsynchronized method.

        return list.size();
    
public intgetUnsyncWaitingThreads()
Return the number of waiting Threads on the queue.

        return waiters;
    
public java.lang.Objectremove(boolean canWait)
Remove a task from the queue. If there are no objects then the thread blocks. The thread will be notified if any object is added to the queue.

return
An object from the queue.

		while (true) {
			if (aborted) {
			    throw new QueueClosedException("Queue closed....");
			}
			synchronized (list) {
			    if (list.size() > 0) {
			        //System.out.println(Thread.currentThread().getName() + ": GOT SOME TASK!!....");
//Bug 4677074 begin
				//_logger.log(Level.FINE,Thread.currentThread().getName() + ": GOT SOME TASK!!....");
//Bug 4677074 end
				    return list.removeFirst();
			    }
    			
			    if (closed) {
			        throw new QueueClosedException("Queue closed....");
			    } else {
			        if (! canWait) {
			            return null;
			        }
			        //System.out.println(Thread.currentThread().getName() + ": waiting....");
//Bug 4677074 begin
				//_logger.log(Level.FINE,Thread.currentThread().getName() + ": waiting....");
//Bug 4677074 end
                                waiters++; // added for 4682740
			        list.wait();
                                waiters--; // added for 4682740
			    }
			}
		}
	
public java.lang.Objectremove(long waitFor)
Remove a task from the queue. If there are no objects then the thread blocks. The thread will be notified if any object is added to the queue.

return
An object from the queue.

        // Fixed for Bug No. 4673949
        if (aborted) {
            throw new QueueClosedException("Queue closed....");
        }
        synchronized (list) {
            if (list.size() > 0) {
                //System.out.println(Thread.currentThread().getName() + ": GOT SOME TASK!!....");
//Bug 4677074 begin
		//_logger.log(Level.FINE,Thread.currentThread().getName() + ": GOT SOME TASK!!....");
//Bug 4677074 end
                return list.removeFirst();
            }

            if (closed) {
                throw new QueueClosedException("Queue closed....");
            } else {
                waiters++; // added for 4682740
                list.wait(waitFor);
                waiters--; // added for 4682740
                if (list.size() > 0) {
                    //System.out.println(Thread.currentThread().getName() + ": GOT SOME TASK!!....");
//Bug 4677074 begin
		    //_logger.log(Level.FINE,Thread.currentThread().getName() + ": GOT SOME TASK!!....");
//Bug 4677074 end
                    return list.removeFirst();
                } else {
                    // We timed out
                    return null;
                }
            }
        }   //Synchronized list
	
public voidshutdown()

		this.closed = true;
		synchronized (list) {
			list.notifyAll();
		}
	
public intsize()

        synchronized (list) {
    	    return list.size();
    	}
    
public java.lang.StringtoString()
Return a String with information about this queue. Good for monitoring.

        StringBuffer sb = new StringBuffer();
        sb.append("BlockingQueue [TW=").append(waiters);
        sb.append(", CS=").append(list.size());
        sb.append(", MS=").append(limit).append("]");
        return sb.toString();