/*
* @(#)file ThreadService.java
* @(#)author Sun Microsystems, Inc.
* @(#)version 1.8
* @(#)date 06/05/03
*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
package com.sun.jmx.snmp.tasks;
import java.util.ArrayList;
import com.sun.jmx.snmp.tasks.Task;
import com.sun.jmx.snmp.tasks.TaskServer;
/**
* This class implements a {@link com.sun.jmx.snmp.tasks.TaskServer} over
* a thread pool.
*
This API is a Sun Microsystems internal API and is subject
* to change without notice.
**/
public class ThreadService implements TaskServer {
public ThreadService(int threadNumber) {
if (threadNumber <= 0) {
throw new IllegalArgumentException("The thread number should bigger than zero.");
}
minThreads = threadNumber;
threadList = new ExecutorThread[threadNumber];
// for (int i=0; i= 0) {
removed = (Runnable)jobList.remove(lg);
}
}
if (removed != null && removed instanceof Task)
((Task) removed).cancel();
return removed;
}
public void removeAll() {
stateCheck();
final Object[] jobs;
synchronized(jobList) {
jobs = jobList.toArray();
jobList.clear();
}
final int len = jobs.length;
for (int i=0; i 0) {
job = (Runnable)jobList.remove(0);
if (jobList.size() > 0) {
jobList.notify();
}
} else {
try {
jobList.wait();
} catch (InterruptedException ie) {
// terminated ?
} finally {
}
continue;
}
}
if (job != null) {
try {
idle--;
job.run();
//System.out.println("jsl-ThreadService: done job "+doneJobs++);
} catch (Exception e) {
// TODO
e.printStackTrace();
} finally {
idle++;
}
}
// re-init
this.setPriority(priority);
this.interrupted();
this.setContextClassLoader(cloader);
}
}
}
// private methods
private void stateCheck() throws IllegalStateException {
if (terminated) {
throw new IllegalStateException("The thread service has been terminated.");
}
}
private void createThread() {
if (idle < 1) {
synchronized(threadList) {
if (jobList.size() > 0 && currThreds < minThreads) {
ExecutorThread et = new ExecutorThread();
et.start();
threadList[currThreds++] = et;
//System.out.println("jsl-ThreadService: create new thread: "+currThreds);
}
}
}
}
// protected or private variables
// ------------------------------
private ArrayList jobList = new ArrayList(0);
private ExecutorThread[] threadList;
private int minThreads = 1;
private int currThreds = 0;
private int idle = 0;
private boolean terminated = false;
private int priority;
private ThreadGroup threadGroup = new ThreadGroup("ThreadService");
private ClassLoader cloader;
private static long counter = 0;
private int addedJobs = 1;
private int doneJobs = 1;
}