FileDocCategorySizeDatePackage
ThreadService.javaAPI DocJava SE 5 API5605Fri Aug 26 14:55:06 BST 2005com.sun.jmx.snmp.tasks

ThreadService

public class ThreadService extends Object implements 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.

Fields Summary
private ArrayList
jobList
private ExecutorThread[]
threadList
private int
minThreads
private int
currThreds
private int
idle
private boolean
terminated
private int
priority
private ThreadGroup
threadGroup
private ClassLoader
cloader
private static long
counter
private int
addedJobs
private int
doneJobs
Constructors Summary
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<threadNumber; i++) {
// 	    threadList[i] = new ExecutorThread();
// 	    threadList[i].start();
// 	}

	priority = Thread.currentThread().getPriority();
	cloader = Thread.currentThread().getContextClassLoader();

//System.out.println("---jsl: ThreadService: running threads = "+threadNumber);
    
Methods Summary
private voidcreateThread()

	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);
		}
	    }
	}
    
public voidremoveAll()

	stateCheck();
	
	final Object[] jobs;
	synchronized(jobList) {
	    jobs = jobList.toArray();
	    jobList.clear();
	}
	final int len = jobs.length;
	for (int i=0; i<len ; i++) {
	    final Object o = jobs[i];
	    if (o!= null && o instanceof Task) ((Task)o).cancel();
	}
    
public java.lang.RunnableremoveTask(java.lang.Runnable task)

	stateCheck();

	Runnable removed = null;
	synchronized(jobList) {
	    int lg = jobList.indexOf(task);
	    if (lg >= 0) {
		removed = (Runnable)jobList.remove(lg);
	    }
	}
	if (removed != null && removed instanceof Task) 
	    ((Task) removed).cancel();
	return removed;
    
private voidstateCheck()

	if (terminated) {
	    throw new IllegalStateException("The thread service has been terminated.");
	}
    
public voidsubmitTask(com.sun.jmx.snmp.tasks.Task task)
Submit a task to be executed. Once a task is submitted, it is guaranteed that either {@link com.sun.jmx.snmp.tasks.Task#run() task.run()} or {@link com.sun.jmx.snmp.tasks.Task#cancel() task.cancel()} will be called. This implementation of TaskServer uses a thread pool to execute the submitted tasks.

param
task The task to be executed.
exception
IllegalArgumentException if the submitted task is null.

	submitTask((Runnable)task);
    
public voidsubmitTask(java.lang.Runnable task)
Submit a task to be executed. This implementation of TaskServer uses a thread pool to execute the submitted tasks.

param
task The task to be executed.
exception
IllegalArgumentException if the submitted task is null.

	stateCheck();

	if (task == null) {
	    throw new IllegalArgumentException("No task specified.");
	}

	synchronized(jobList) {
	    jobList.add(jobList.size(), task);
//System.out.println("jsl-ThreadService: added job "+addedJobs++);

	    jobList.notify();
	}

	createThread();
    
public voidterminate()


	if (terminated == true) {
	    return;
	}

	terminated = true;

	synchronized(jobList) {
	    jobList.notifyAll();
	}

	removeAll();

	for (int i=0; i<currThreds; i++) {
	    try {
		threadList[i].interrupt();
	    } catch (Exception e) {
		// TODO
	    }
	}

	threadList = null;