FileDocCategorySizeDatePackage
WorkQueueImpl.javaAPI DocJava SE 5 API5585Fri Aug 26 14:54:30 BST 2005com.sun.corba.se.impl.orbutil.threadpool

WorkQueueImpl

public class WorkQueueImpl extends Object implements com.sun.corba.se.spi.orbutil.threadpool.WorkQueue

Fields Summary
private com.sun.corba.se.spi.orbutil.threadpool.ThreadPool
workerThreadPool
private LinkedList
theWorkQueue
private long
workItemsAdded
private long
workItemsDequeued
private long
totalTimeInQueue
private String
name
private com.sun.corba.se.spi.monitoring.MonitoredObject
workqueueMonitoredObject
Constructors Summary
public WorkQueueImpl()


      
	name=ORBConstants.WORKQUEUE_DEFAULT_NAME;
	initializeMonitoring();
    
public WorkQueueImpl(com.sun.corba.se.spi.orbutil.threadpool.ThreadPool workerThreadPool)

        this(workerThreadPool, ORBConstants.WORKQUEUE_DEFAULT_NAME);
    
public WorkQueueImpl(com.sun.corba.se.spi.orbutil.threadpool.ThreadPool workerThreadPool, String name)

        this.workerThreadPool = workerThreadPool;
	this.name = name;
	initializeMonitoring();
    
Methods Summary
public voidaddWork(com.sun.corba.se.spi.orbutil.threadpool.Work work)

        synchronized (this) {
            workItemsAdded++;
            work.setEnqueueTime(System.currentTimeMillis());
            theWorkQueue.addLast(work);
	    ((ThreadPoolImpl)workerThreadPool).notifyForAvailableWork(this);
        }
    
public synchronized longaverageTimeInQueue()

        return (totalTimeInQueue/workItemsDequeued);
    
com.sun.corba.se.spi.monitoring.MonitoredObjectgetMonitoredObject()

	return workqueueMonitoredObject;
    
public java.lang.StringgetName()

        return name;
    
public com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolgetThreadPool()

	    return workerThreadPool;
    
private voidinitializeMonitoring()

	workqueueMonitoredObject = MonitoringFactories.
			    getMonitoredObjectFactory().
			    createMonitoredObject(name,
			    MonitoringConstants.WORKQUEUE_MONITORING_DESCRIPTION);

	LongMonitoredAttributeBase b1 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_TOTAL_WORK_ITEMS_ADDED, 
		    MonitoringConstants.WORKQUEUE_TOTAL_WORK_ITEMS_ADDED_DESCRIPTION) {
		public Object getValue() {
		    return new Long(WorkQueueImpl.this.totalWorkItemsAdded());
		}
	    };
	workqueueMonitoredObject.addAttribute(b1);
	LongMonitoredAttributeBase b2 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_WORK_ITEMS_IN_QUEUE, 
		    MonitoringConstants.WORKQUEUE_WORK_ITEMS_IN_QUEUE_DESCRIPTION) {
		public Object getValue() {
		    return new Long(WorkQueueImpl.this.workItemsInQueue());
		}
	    };
	workqueueMonitoredObject.addAttribute(b2);
	LongMonitoredAttributeBase b3 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE, 
		    MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE_DESCRIPTION) {
		public Object getValue() {
		    return new Long(WorkQueueImpl.this.averageTimeInQueue());
		}
	    };
	workqueueMonitoredObject.addAttribute(b3);
    
com.sun.corba.se.spi.orbutil.threadpool.WorkrequestWork(long waitTime)

        Work workItem;
        synchronized (this) {
            if (theWorkQueue.size() != 0) {
                workItem = (Work)theWorkQueue.removeFirst();
                totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
                workItemsDequeued++;
                return workItem;
            }

            try {

                long remainingWaitTime = waitTime;
                long finishTime = System.currentTimeMillis() + waitTime;

                do {

                    this.wait(remainingWaitTime);

                    if (theWorkQueue.size() != 0) {
                        workItem = (Work)theWorkQueue.removeFirst();
                        totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
                        workItemsDequeued++;
                        return workItem;
                    }

                    remainingWaitTime = finishTime - System.currentTimeMillis();

                } while (remainingWaitTime > 0);

                throw new TimeoutException();

            } catch (InterruptedException ie) {
                throw ie;
            }
        }
    
public voidsetThreadPool(com.sun.corba.se.spi.orbutil.threadpool.ThreadPool workerThreadPool)

	    this.workerThreadPool = workerThreadPool;
    
public longtotalWorkItemsAdded()
Returns the total number of Work items added to the Queue. This method is unsynchronized and only gives a snapshot of the state when it is called

        return workItemsAdded;
    
public intworkItemsInQueue()
Returns the total number of Work items in the Queue to be processed This method is unsynchronized and only gives a snapshot of the state when it is called

        return theWorkQueue.size();