1 22 package fr.dyade.aaa.util; 23 24 import org.objectweb.util.monolog.api.BasicLevel; 25 import org.objectweb.util.monolog.api.Logger; 26 27 56 public abstract class Daemon implements Runnable { 57 58 62 public synchronized boolean isRunning() { 63 return ((thread != null) && thread.isAlive()); 64 } 65 66 72 protected volatile boolean running; 73 80 protected volatile boolean canStop; 81 82 protected Thread thread = null; 83 84 private String name; 85 86 private boolean daemon = false; 87 88 protected int priority = Thread.NORM_PRIORITY; 89 90 protected ThreadGroup group; 91 92 97 public final String getName() { 98 return name; 99 } 100 101 106 public String toString() { 107 StringBuffer strbuf = new StringBuffer (); 108 109 strbuf.append('(').append(super.toString()); 110 strbuf.append(",name=").append(getName()); 111 strbuf.append(",running=").append(running); 112 strbuf.append(",canStop=").append(canStop); 113 strbuf.append(",thread=").append(thread); 114 strbuf.append(')'); 115 116 return strbuf.toString(); 117 } 118 119 protected Logger logmon = null; 120 121 126 protected Daemon(String name) { 127 this(name, null); 128 } 129 130 136 protected Daemon(String name, Logger logmon) { 137 this.name = name; 138 139 if (logmon == null) { 140 this.logmon = Debug.getLogger(getClass().getName() + '.' + name); 142 this.logmon.log(BasicLevel.DEBUG, getName() + ", created."); 143 } else { 144 this.logmon = logmon; 145 } 146 147 running = false; 148 canStop = false; 149 thread = null; 150 } 151 152 158 public void setDaemon(boolean daemon) { 159 if (running || ((thread != null) && thread.isAlive())) { 160 throw new IllegalThreadStateException ("already started"); 161 } 162 this.daemon = daemon; 163 } 164 165 173 public void setPriority(int newPriority) { 174 if ((newPriority > Thread.MAX_PRIORITY) || 175 (newPriority < Thread.MIN_PRIORITY)) { 176 throw new IllegalArgumentException (); 177 } 178 if (running && (thread != null) && thread.isAlive()) 179 thread.setPriority(newPriority); 180 priority = newPriority; 181 } 182 183 189 public void setThreadGroup(ThreadGroup group) { 190 if (running || ((thread != null) && thread.isAlive())) { 191 throw new IllegalThreadStateException ("already started"); 192 } 193 this.group = group; 194 } 195 196 203 public synchronized void start() { 204 if ((thread != null) && thread.isAlive()) { 205 logmon.log(BasicLevel.WARN, getName() + ", already started."); 206 throw new IllegalThreadStateException ("already started"); 207 } 208 209 thread = new Thread (group, this, getName()); 210 thread.setDaemon(daemon); 211 if (priority != Thread.NORM_PRIORITY) 212 thread.setPriority(priority); 213 running = true; 214 canStop = true; 215 thread.start(); 216 217 logmon.log(BasicLevel.DEBUG, getName() + ", started."); 218 } 219 220 224 protected abstract void close(); 225 226 233 protected abstract void shutdown(); 234 235 238 public void interrupt() { 239 thread.interrupt(); 240 } 241 242 final protected void finish() { 243 running = false; 244 close(); 245 logmon.log(BasicLevel.DEBUG, getName() + ", ended"); 246 } 247 248 253 public synchronized void stop() { 254 logmon.log(BasicLevel.DEBUG, getName() + ", stops."); 255 running = false; 256 if (thread != null) { 257 while (thread.isAlive()) { 258 if (canStop) { 259 260 if (thread.isAlive()) 261 thread.interrupt(); 262 263 shutdown(); 264 } 265 try { 266 thread.join(1000L); 267 } catch (InterruptedException exc) { 268 continue; 269 } 270 } 271 finish(); 272 thread = null; 273 } 274 } 275 276 279 public boolean isCurrentThread() { 280 return ((thread != null) && (thread == Thread.currentThread())); 281 } 282 283 public void setName(String name) { 284 thread.setName(name); 285 } 286 } 287 | Popular Tags |