1 11 12 package com.sun.jmx.snmp.tasks; 13 14 import java.util.ArrayList ; 15 import com.sun.jmx.snmp.tasks.Task; 16 import com.sun.jmx.snmp.tasks.TaskServer; 17 18 24 public class ThreadService implements TaskServer { 25 26 public ThreadService(int threadNumber) { 27 if (threadNumber <= 0) { 28 throw new IllegalArgumentException ("The thread number should bigger than zero."); 29 } 30 31 minThreads = threadNumber; 32 threadList = new ExecutorThread[threadNumber]; 33 34 39 priority = Thread.currentThread().getPriority(); 40 cloader = Thread.currentThread().getContextClassLoader(); 41 42 } 44 45 48 58 public void submitTask(Task task) throws IllegalArgumentException { 59 submitTask((Runnable )task); 60 } 61 62 69 public void submitTask(Runnable task) throws IllegalArgumentException { 70 stateCheck(); 71 72 if (task == null) { 73 throw new IllegalArgumentException ("No task specified."); 74 } 75 76 synchronized(jobList) { 77 jobList.add(jobList.size(), task); 78 80 jobList.notify(); 81 } 82 83 createThread(); 84 } 85 86 public Runnable removeTask(Runnable task) { 87 stateCheck(); 88 89 Runnable removed = null; 90 synchronized(jobList) { 91 int lg = jobList.indexOf(task); 92 if (lg >= 0) { 93 removed = (Runnable )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 [] 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 o = jobs[i]; 112 if (o!= null && o instanceof Task) ((Task)o).cancel(); 113 } 114 } 115 116 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 e) { 135 } 137 } 138 139 threadList = null; 140 } 141 142 145 private class ExecutorThread extends Thread { 148 public ExecutorThread() { 149 super(threadGroup, "ThreadService-"+counter++); 150 setDaemon(true); 151 152 this.setPriority(priority); 154 this.setContextClassLoader(cloader); 155 156 idle++; 157 } 158 159 public void run() { 160 161 while(!terminated) { 162 Runnable job = null; 163 164 synchronized(jobList) { 165 if (jobList.size() > 0) { 166 job = (Runnable )jobList.remove(0); 167 if (jobList.size() > 0) { 168 jobList.notify(); 169 } 170 171 } else { 172 try { 173 jobList.wait(); 174 } catch (InterruptedException ie) { 175 } finally { 177 } 178 continue; 179 } 180 } 181 if (job != null) { 182 try { 183 idle--; 184 job.run(); 185 187 } catch (Exception e) { 188 e.printStackTrace(); 190 } finally { 191 idle++; 192 } 193 } 194 195 this.setPriority(priority); 197 this.interrupted(); 198 this.setContextClassLoader(cloader); 199 } 200 } 201 } 202 203 private void stateCheck() throws IllegalStateException { 205 if (terminated) { 206 throw new IllegalStateException ("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 } 219 } 220 } 221 } 222 223 224 private ArrayList jobList = new ArrayList (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 threadGroup = new ThreadGroup ("ThreadService"); 236 private ClassLoader cloader; 237 238 private static long counter = 0; 239 240 private int addedJobs = 1; 241 private int doneJobs = 1; 242 } 243 | Popular Tags |