/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007 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: JSSThreadPool.java,v 1.3 2008/06/25 05:41:34 qcheng Exp $
*
*/
public class JSSThreadPool {
private int poolSize;
private int busyThreadCount;
private int currentThreadCount;
private int busyReaderWriterCount;
private int currentReaderWriterCount;
private boolean shutdownThePool;
private boolean daemon;
/**
* 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 daemon
* set the threads as daemon if true; otherwise if not.
* @param debug
* Debug object to send debugging message to.
*/
// initialize the size of the ArrayList, it doesn't need to expand
// during runtime.
this.busyThreadCount = 0;
this.currentThreadCount = 0;
this.busyReaderWriterCount = 0;
this.currentReaderWriterCount = 0;
this.shutdownThePool = false;
synchronized (this) {
}
}
/**
* Create thread for the pool.
*
* @param threadsToCreate number of threads of the pool after creation
*/
if (threadsToCreate > poolSize) {
}
for (int i = currentThreadCount; i < threadsToCreate; i++) {
}
}
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 (busyThreadCount == poolSize) {
}
else{
t = getAvailableThread();
}
}
}
}
protected synchronized void deductCurrentThreadCount(){
WorkerThread t = getAvailableThread();
}
}
// return the thread to the thread pool
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.
t.setNeedReturn(false);
if(busyThreadCount == 0){
notify();
}
return;
}
}
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;
// 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 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;
this.notify();
}
// this will be set by ThreadPool.returnThread when shutdown is
// called.
needReturn = value;
}
}
}