FileDocCategorySizeDatePackage
StoppableTaskQueue.javaAPI DocExample1461Mon Nov 13 12:11:54 GMT 2006collections

StoppableTaskQueue

public class StoppableTaskQueue extends Object

Fields Summary
private final int
MAXIMUM_PENDING_OFFERS
private final BlockingQueue
taskQueue
private boolean
isStopped
private Semaphore
semaphore
Constructors Summary
Methods Summary
public booleanaddTask(PriorityTask task)

  // return true if the task was successfully placed on the queue, false
  // if the queue has been shut down.
      
    synchronized (this) {
      if (isStopped) return false;
      if (! semaphore.tryAcquire()) throw new Error("too many threads");
    }
    try {
      return taskQueue.offer(task);
    } finally {
      semaphore.release();
    }
  
public PriorityTaskgetTask()

    return taskQueue.poll();
  
public java.util.CollectionshutDown()

    synchronized(this) { isStopped = true; }
    semaphore.acquireUninterruptibly(MAXIMUM_PENDING_OFFERS);
    Set<PriorityTask> returnCollection = new HashSet<PriorityTask>();
    taskQueue.drainTo(returnCollection);
    return returnCollection;