FileDocCategorySizeDatePackage
ThreadPoolImpl.javaAPI DocJava SE 6 API15012Tue Jun 10 00:21:40 BST 2008com.sun.corba.se.impl.orbutil.threadpool

ThreadPoolImpl

public class ThreadPoolImpl extends Object implements com.sun.corba.se.spi.orbutil.threadpool.ThreadPool

Fields Summary
private static int
threadCounter
private com.sun.corba.se.spi.orbutil.threadpool.WorkQueue
workQueue
private int
availableWorkerThreads
private int
currentThreadCount
private int
minWorkerThreads
private int
maxWorkerThreads
private long
inactivityTimeout
private boolean
boundedThreadPool
private long
processedCount
private long
totalTimeTaken
private Object
lock
private String
name
private com.sun.corba.se.spi.monitoring.MonitoredObject
threadpoolMonitoredObject
private ThreadGroup
threadGroup
Constructors Summary
public ThreadPoolImpl(ThreadGroup tg, String threadpoolName)
This constructor is used to create an unbounded threadpool


                  
         
        inactivityTimeout = ORBConstants.DEFAULT_INACTIVITY_TIMEOUT;
        maxWorkerThreads = Integer.MAX_VALUE;
        workQueue = new WorkQueueImpl(this);
	threadGroup = tg ;
	name = threadpoolName;
	initializeMonitoring();
    
public ThreadPoolImpl(String threadpoolName)
This constructor is used to create an unbounded threadpool in the ThreadGroup of the current thread

	this( Thread.currentThread().getThreadGroup(), threadpoolName ) ; 
    
public ThreadPoolImpl(int minSize, int maxSize, long timeout, String threadpoolName)
This constructor is used to create bounded threadpool

        minWorkerThreads = minSize;
        maxWorkerThreads = maxSize;
        inactivityTimeout = timeout;
        boundedThreadPool = true;
        workQueue = new WorkQueueImpl(this);
	name = threadpoolName;
        for (int i = 0; i < minWorkerThreads; i++) {
            createWorkerThread();
        }
	initializeMonitoring();
    
Methods Summary
public longaverageWorkCompletionTime()
This method returns the average elapsed time taken to complete a Work item in milliseconds.

	synchronized (lock) {
	    return (totalTimeTaken / processedCount);
	}
    
voidcreateWorkerThread()
To be called from the workqueue to create worker threads when none available.

        WorkerThread thread;
        
	synchronized (lock) {
	    if (boundedThreadPool) {
		if (currentThreadCount < maxWorkerThreads) {
		    thread = new WorkerThread(threadGroup, getName());
		    currentThreadCount++;
		} else {
		    // REVIST - Need to create a thread to monitor the
		    // the state for deadlock i.e. all threads waiting for
		    // something which can be got from the item in the 
		    // workqueue, but there is no thread available to
		    // process that work item - DEADLOCK !!
		    return;
		}
	    } else {
		thread = new WorkerThread(threadGroup, getName());
		currentThreadCount++;
	    }
	}
        
        // The thread must be set to a daemon thread so the
        // VM can exit if the only threads left are PooledThreads
        // or other daemons.  We don't want to rely on the
        // calling thread always being a daemon.

        // Catch exceptions since setDaemon can cause a
        // security exception to be thrown under netscape
        // in the Applet mode
        try {
            thread.setDaemon(true);
        } catch (Exception e) {
	    // REVISIT - need to do some logging here
	}

        thread.start();
    
public intcurrentNumberOfThreads()
This method will return the total number of threads currently in the threadpool. This method returns a value which is not synchronized.

	synchronized (lock) {
	    return currentThreadCount;
	}
    
public longcurrentProcessedCount()
This method returns the number of Work items processed by the threadpool

	synchronized (lock) {
	    return processedCount;
	}
    
public com.sun.corba.se.spi.orbutil.threadpool.WorkQueuegetAnyWorkQueue()

	return workQueue;
    
com.sun.corba.se.spi.monitoring.MonitoredObjectgetMonitoredObject()

	return threadpoolMonitoredObject;
    
public java.lang.StringgetName()

        return name;
    
private static synchronized intgetUniqueThreadId()

        return ThreadPoolImpl.threadCounter++;
    
public com.sun.corba.se.spi.orbutil.threadpool.WorkQueuegetWorkQueue(int queueId)

	if (queueId != 0)
	    throw new NoSuchWorkQueueException();
	return workQueue;
    
public longidleTimeoutForThreads()
This method will return the time in milliseconds when idle threads in the threadpool are removed.

        return inactivityTimeout;
    
private voidinitializeMonitoring()

	// Get root monitored object
	MonitoredObject root = MonitoringFactories.getMonitoringManagerFactory().
		createMonitoringManager(MonitoringConstants.DEFAULT_MONITORING_ROOT, null).
		getRootMonitoredObject();

	// Create the threadpool monitoring root
	MonitoredObject threadPoolMonitoringObjectRoot = root.getChild(
		    MonitoringConstants.THREADPOOL_MONITORING_ROOT);
	if (threadPoolMonitoringObjectRoot == null) {
	    threadPoolMonitoringObjectRoot =  MonitoringFactories.
		    getMonitoredObjectFactory().createMonitoredObject(
		    MonitoringConstants.THREADPOOL_MONITORING_ROOT,
		    MonitoringConstants.THREADPOOL_MONITORING_ROOT_DESCRIPTION);
	    root.addChild(threadPoolMonitoringObjectRoot);
	}
	threadpoolMonitoredObject = MonitoringFactories.
		    getMonitoredObjectFactory().
		    createMonitoredObject(name,
		    MonitoringConstants.THREADPOOL_MONITORING_DESCRIPTION);

	threadPoolMonitoringObjectRoot.addChild(threadpoolMonitoredObject);

	LongMonitoredAttributeBase b1 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS, 
		    MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS_DESCRIPTION) {
		public Object getValue() {
		    return new Long(ThreadPoolImpl.this.currentNumberOfThreads());
		}
	    };
	threadpoolMonitoredObject.addAttribute(b1);
	LongMonitoredAttributeBase b2 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_NUMBER_OF_AVAILABLE_THREADS, 
		    MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS_DESCRIPTION) {
		public Object getValue() {
		    return new Long(ThreadPoolImpl.this.numberOfAvailableThreads());
		}
	    };
	threadpoolMonitoredObject.addAttribute(b2);
	LongMonitoredAttributeBase b3 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_NUMBER_OF_BUSY_THREADS, 
		    MonitoringConstants.THREADPOOL_NUMBER_OF_BUSY_THREADS_DESCRIPTION) {
		public Object getValue() {
		    return new Long(ThreadPoolImpl.this.numberOfBusyThreads());
		}
	    };
	threadpoolMonitoredObject.addAttribute(b3);
	LongMonitoredAttributeBase b4 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_AVERAGE_WORK_COMPLETION_TIME, 
		    MonitoringConstants.THREADPOOL_AVERAGE_WORK_COMPLETION_TIME_DESCRIPTION) {
		public Object getValue() {
		    return new Long(ThreadPoolImpl.this.averageWorkCompletionTime());
		}
	    };
	threadpoolMonitoredObject.addAttribute(b4);
	LongMonitoredAttributeBase b5 = new 
	    LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_CURRENT_PROCESSED_COUNT, 
		    MonitoringConstants.THREADPOOL_CURRENT_PROCESSED_COUNT_DESCRIPTION) {
		public Object getValue() {
		    return new Long(ThreadPoolImpl.this.currentProcessedCount());
		}
	    };
	threadpoolMonitoredObject.addAttribute(b5);

	// Add the monitored object for the WorkQueue
	
	threadpoolMonitoredObject.addChild(
		((WorkQueueImpl)workQueue).getMonitoredObject());
    
public intmaximumNumberOfThreads()
This method will return the maximum number of threads in the threadpool at any point in time, for the life of the threadpool

        return maxWorkerThreads;
    
public intminimumNumberOfThreads()
This method will return the minimum number of threads maintained by the threadpool.

        return minWorkerThreads;
    
voidnotifyForAvailableWork(com.sun.corba.se.spi.orbutil.threadpool.WorkQueue aWorkQueue)
To be called from the workqueue when work is added to the workQueue. This method would create new threads if required or notify waiting threads on the queue for available work

	synchronized (lock) {
	    if (availableWorkerThreads == 0) {
		createWorkerThread();
	    } else {
		aWorkQueue.notify();
	    }
	}
    
public intnumberOfAvailableThreads()
This method will return the number of available threads in the threadpool which are waiting for work. This method returns a value which is not synchronized.

	synchronized (lock) {
	    return availableWorkerThreads;
	}
    
public intnumberOfBusyThreads()
This method will return the number of busy threads in the threadpool This method returns a value which is not synchronized.

	synchronized (lock) {
	    return (currentThreadCount - availableWorkerThreads);
	}
    
public intnumberOfWorkQueues()
This method will return the number of WorkQueues serviced by the threadpool.

        return 1;