KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > tasks > ThreadService


1 /*
2  * @(#)file ThreadService.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.8
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12 package com.sun.jmx.snmp.tasks;
13
14 import java.util.ArrayList JavaDoc;
15 import com.sun.jmx.snmp.tasks.Task;
16 import com.sun.jmx.snmp.tasks.TaskServer;
17
18 /**
19  * This class implements a {@link com.sun.jmx.snmp.tasks.TaskServer} over
20  * a thread pool.
21  * <p><b>This API is a Sun Microsystems internal API and is subject
22  * to change without notice.</b></p>
23  **/

24 public class ThreadService implements TaskServer {
25
26     public ThreadService(int threadNumber) {
27     if (threadNumber <= 0) {
28         throw new IllegalArgumentException JavaDoc("The thread number should bigger than zero.");
29     }
30
31     minThreads = threadNumber;
32     threadList = new ExecutorThread[threadNumber];
33
34 // for (int i=0; i<threadNumber; i++) {
35
// threadList[i] = new ExecutorThread();
36
// threadList[i].start();
37
// }
38

39     priority = Thread.currentThread().getPriority();
40     cloader = Thread.currentThread().getContextClassLoader();
41
42 //System.out.println("---jsl: ThreadService: running threads = "+threadNumber);
43
}
44
45 // public methods
46
// --------------
47

48     /**
49      * Submit a task to be executed.
50      * Once a task is submitted, it is guaranteed that either
51      * {@link com.sun.jmx.snmp.tasks.Task#run() task.run()} or
52      * {@link com.sun.jmx.snmp.tasks.Task#cancel() task.cancel()} will be called.
53      * This implementation of TaskServer uses a thread pool to execute
54      * the submitted tasks.
55      * @param task The task to be executed.
56      * @exception IllegalArgumentException if the submitted task is null.
57      **/

58     public void submitTask(Task task) throws IllegalArgumentException JavaDoc {
59     submitTask((Runnable JavaDoc)task);
60     }
61
62     /**
63      * Submit a task to be executed.
64      * This implementation of TaskServer uses a thread pool to execute
65      * the submitted tasks.
66      * @param task The task to be executed.
67      * @exception IllegalArgumentException if the submitted task is null.
68      **/

69     public void submitTask(Runnable JavaDoc task) throws IllegalArgumentException JavaDoc {
70     stateCheck();
71
72     if (task == null) {
73         throw new IllegalArgumentException JavaDoc("No task specified.");
74     }
75
76     synchronized(jobList) {
77         jobList.add(jobList.size(), task);
78 //System.out.println("jsl-ThreadService: added job "+addedJobs++);
79

80         jobList.notify();
81     }
82
83     createThread();
84     }
85
86     public Runnable JavaDoc removeTask(Runnable JavaDoc task) {
87     stateCheck();
88
89     Runnable JavaDoc removed = null;
90     synchronized(jobList) {
91         int lg = jobList.indexOf(task);
92         if (lg >= 0) {
93         removed = (Runnable JavaDoc)jobList.remove(lg);
94         }
95     }
96     if (removed != null && removed instanceof Task)
97         ((Task) removed).cancel();
98     return removed;
99     }
100
101     public void removeAll() {
102     stateCheck();
103     
104     final Object JavaDoc[] jobs;
105     synchronized(jobList) {
106         jobs = jobList.toArray();
107         jobList.clear();
108     }
109     final int len = jobs.length;
110     for (int i=0; i<len ; i++) {
111         final Object JavaDoc o = jobs[i];
112         if (o!= null && o instanceof Task) ((Task)o).cancel();
113     }
114     }
115
116     // to terminate
117
public void terminate() {
118
119     if (terminated == true) {
120         return;
121     }
122
123     terminated = true;
124
125     synchronized(jobList) {
126         jobList.notifyAll();
127     }
128
129     removeAll();
130
131     for (int i=0; i<currThreds; i++) {
132         try {
133         threadList[i].interrupt();
134         } catch (Exception JavaDoc e) {
135         // TODO
136
}
137     }
138
139     threadList = null;
140     }
141
142 // private classes
143
// ---------------
144

145     // A thread used to execute jobs
146
//
147
private class ExecutorThread extends Thread JavaDoc {
148     public ExecutorThread() {
149         super(threadGroup, "ThreadService-"+counter++);
150         setDaemon(true);
151
152         // init
153
this.setPriority(priority);
154         this.setContextClassLoader(cloader);
155  
156         idle++;
157     }
158
159     public void run() {
160
161         while(!terminated) {
162         Runnable JavaDoc job = null;
163
164         synchronized(jobList) {
165             if (jobList.size() > 0) {
166                 job = (Runnable JavaDoc)jobList.remove(0);
167             if (jobList.size() > 0) {
168                 jobList.notify();
169             }
170             
171             } else {
172             try {
173                 jobList.wait();
174             } catch (InterruptedException JavaDoc ie) {
175                 // terminated ?
176
} finally {
177             }
178             continue;
179             }
180         }
181         if (job != null) {
182             try {
183             idle--;
184             job.run();
185 //System.out.println("jsl-ThreadService: done job "+doneJobs++);
186

187             } catch (Exception JavaDoc e) {
188             // TODO
189
e.printStackTrace();
190             } finally {
191             idle++;
192             }
193         }
194
195         // re-init
196
this.setPriority(priority);
197         this.interrupted();
198         this.setContextClassLoader(cloader);
199         }
200     }
201     }
202
203 // private methods
204
private void stateCheck() throws IllegalStateException JavaDoc {
205     if (terminated) {
206         throw new IllegalStateException JavaDoc("The thread service has been terminated.");
207     }
208     }
209
210     private void createThread() {
211     if (idle < 1) {
212         synchronized(threadList) {
213         if (jobList.size() > 0 && currThreds < minThreads) {
214             ExecutorThread et = new ExecutorThread();
215             et.start();
216             threadList[currThreds++] = et;
217 //System.out.println("jsl-ThreadService: create new thread: "+currThreds);
218
}
219         }
220     }
221     }
222
223
224 // protected or private variables
225
// ------------------------------
226
private ArrayList JavaDoc jobList = new ArrayList JavaDoc(0);
227
228     private ExecutorThread[] threadList;
229     private int minThreads = 1;
230     private int currThreds = 0;
231     private int idle = 0;
232
233     private boolean terminated = false;
234     private int priority;
235     private ThreadGroup JavaDoc threadGroup = new ThreadGroup JavaDoc("ThreadService");
236     private ClassLoader JavaDoc cloader;
237
238     private static long counter = 0;
239
240     private int addedJobs = 1;
241     private int doneJobs = 1;
242 }
243
Popular Tags