ThreadPool.java revision 8af80418ba1ec431c8027fa9668e5678658d3611
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: ThreadPool.java,v 1.10 2008/10/04 00:11:46 arviranga Exp $
*
*/
/**
* <p>
* This thread pool maintains a number of threads that run the tasks from a task
* queue one by one. The tasks are handled in asynchronous mode, which means it
* will not block the main thread to proceed while the task is being processed
* by the thread pool.
* <p>
* This thread pool has a fixed size of threads. It maintains all the tasks to
* be executed in a task queue. Each thread then in turn gets a task from the
* queue to execute. If the tasks in the task queue reaches a certain number(the
* threshold value), it will log an error message and ignore the new incoming
* tasks until the number of un-executed tasks is less than the threshold value.
* This guarantees the thread pool will not use up the system resources under
* heavy load.
* @supported.all.api
*/
public class ThreadPool {
private int poolSize;
private int threshold;
private int busyThreadCount;
private int currentThreadCount;
private boolean shutdownThePool;
private boolean daemon;
private WorkerThread[] threads;
/**
* Constructs a thread pool with given parameters.
*
* @param name
* name of the thread pool.
* @param poolSize
* the thread pool size, indicates how many threads are created
* in the pool.
* @param threshold
* the maximum size of the task queue in the thread pool.
* @param daemon
* set the threads as daemon if true; otherwise if not.
* @param debug
* Debug object to send debugging message to.
*/
if (threshold > 0) {
// initialize the size of the ArrayList, it doesn't need to expand
// during runtime.
} else {
}
this.busyThreadCount = 0;
this.currentThreadCount = 0;
this.shutdownThePool = false;
if (debug.messageEnabled()) {
}
synchronized (this) {
}
}
/**
* Create thread for the pool.
*
* @param threadsToCreate number of threads of the pool after creation
*/
protected void createThreads(int threadsToCreate) {
if (threadsToCreate > poolSize) {
}
for (int i = currentThreadCount; i < threadsToCreate; i++) {
}
}
private WorkerThread getAvailableThread() {
WorkerThread t = null;
synchronized (this) {
if (currentThreadCount == busyThreadCount) {
}
// get threads from the end of the array
}
return t;
}
/**
* Runs a user defined task.
*
* @param task
* user defined task.
* @throws ThreadPoolException
*/
{
WorkerThread t = null;
synchronized (this) {
if (shutdownThePool) {
// No more tasks will be accepted
throw new ThreadPoolException(poolName +
" thread pool's being shutdown.");
}
if (busyThreadCount == poolSize) {
throw new ThreadPoolException(poolName +
" thread pool's task queue is full.");
} else {
}
}
else{
t = getAvailableThread();
}
}
}
}
protected synchronized void deductCurrentThreadCount(){
WorkerThread t = getAvailableThread();
} else {
notify();
}
}
}
// return the thread to the thread pool
protected synchronized void returnThread(WorkerThread t) {
}
else{
if(shutdownThePool) {
t.terminate();
// notify the thread pool when all threads are backed
// need to discuss whether the thread pool need to wait until
// all threads are terminated. For stand alone application, the
// answer is yes, however, our application is run under web
// container. The reason why we need shutdown because it has a
// parameter daemon in the constructor, if it is set to false,
// the old implementation has no way to stop the running
// threads. For the new implementation, if daemon is set to
// false, it is necessary to call shutdown. If daemon is set to
// true, it is nice to call it because the thread pool has
// better knownledge than the web container to stop the threads
// in the pool.
if(busyThreadCount == 0){
notify();
}
} else {
// return threads from the end of array
}
}
}
// terminate all the threads since the pass-in parameter of daemon may be
// false
public synchronized void shutdown() {
if(!shutdownThePool) {
shutdownThePool = true;
// If daemon thread, discard the remaining tasks
// else, wait for all tasks to be completed
if (daemon) {
} else {
try {
// wait if there are tasks & threads to be executed
wait();
}
}
}
// terminate the thread from the beginning of the array
}
}
while(busyThreadCount != 0){
try{
// wait if there are threads running, it will be notified
// when they all back.
wait();
}
}
}
}
// for test only
public synchronized int getCurrentThreadCount() {
return currentThreadCount;
}
/*
* Returns the size of the task list.
*/
public int getCurrentSize() {
}
// private thread class that fetches tasks from the task queue and
// executes them.
private class WorkerThread extends Thread {
private ThreadPool pool;
private boolean needReturn;
private boolean shouldTerminate;
this.shouldTerminate = false;
this.needReturn = true;
}
/**
* Starts the thread pool.
*/
public void run() {
boolean localShouldTerminate = false;
WorkerThread t = this;
while (true) {
try{
synchronized (this) {
this.wait();
}
// need a local copy because they may be changed after
// leaving synchronized block.
}
if (localShouldTerminate) {
// we may need to log something here!
break;
}
}
} catch (RuntimeException ex) {
// decide what to log here
localShouldTerminate = true;
needReturn = false;
// don't need to rethrow
} catch (Throwable e) {
// decide what to log here
localShouldTerminate = true;
needReturn = false;
// rethrow Error here
throw new Error(e);
} finally {
// the thread may has returned already if shutdown is
// called.
if (needReturn) {
pool.returnThread(t);
}
}
if (localShouldTerminate) {
// we may need to log something here!
break;
}
}
}
// Although the thread may not in wait state when this function
// is called (the taskList is not empty), it doesn't hurt to
// call it. getState method can check whether the Thread is
// waiting, but it is available in jdk1.5 or newer.
this.notify();
}
// terminate the thread pool when daemon is set to false
// it is better to have a way to terminate the thread pool
public synchronized void terminate() {
shouldTerminate = true;
needReturn = false;
this.notify();
}
}
}