1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.utility; 31 32 74 public class Timer extends Object implements Runnable 75 { 76 79 private Thread runner; 80 81 84 private boolean active; 85 86 89 private boolean tickOnce; 90 91 94 private long interval; 95 96 99 private long ticks; 100 101 104 private TickEventListener owner; 105 106 109 private boolean isTerminated; 110 111 117 public static int suspends = 0; 118 119 125 public static int resumes = 0; 126 127 128 138 public Timer(TickEventListener obj, long interval, boolean active) 139 { 140 this(obj, interval, active, false, null); 141 } 142 143 public Timer(TickEventListener obj, long interval, boolean active, 144 boolean tickOnce, String name) 145 { 146 Thread thd; 147 this.owner = obj; 148 this.interval = interval; 149 this.active = active; 150 this.tickOnce = tickOnce; 151 this.isTerminated = false; 152 thd = new Thread (this); 153 thd.setDaemon(true); 154 if (name != null) 155 { 156 thd.setName(name); 157 } 158 else 159 { 160 thd.setName("TimerThread" + System.identityHashCode(this)); } 162 thd.start(); 163 } 164 165 170 public void setInterval(long interval) 171 { 172 synchronized(this) 173 { 174 if (this.interval == interval) 175 return; 176 177 this.interval = interval; 178 179 if (this.active) 180 this.runner.interrupt(); 181 } 182 } 183 184 189 public long getInterval() 195 { 196 return this.interval; 197 } 198 199 203 public long getTickCount() 209 { 210 return this.ticks; 211 } 212 213 219 public void setTickCount(long count) 225 { 226 this.ticks = count; 227 } 228 229 236 public void setState(boolean state) 242 { 243 synchronized(this) 244 { 245 if (this.active == state) 246 return; 247 248 if (this.active) 249 this.runner.interrupt(); 250 else 251 this.notify(); 252 253 this.active = state; 254 } 255 } 256 257 260 public void terminate() 266 { 267 synchronized(this) 268 { 269 this.isTerminated = true; 270 if (this.runner != null) 271 this.setState(false); 272 } 273 } 274 275 278 public void run() 279 { 280 try 281 { 282 this.runMe(); 283 } 284 finally 285 { 286 this.cleanForGC(); 287 } 288 } 289 290 293 void runMe() 294 { 295 378 } 379 380 383 private void cleanForGC() 384 { 385 this.owner = null; 386 this.runner = null; 387 } 388 389 392 public void destroy() 393 { 394 try 395 { 396 this.cleanForGC(); 397 Thread runThread; 398 if ((runThread = this.runner) != null) 399 { 400 } 402 } 403 catch (Exception e) 404 { 405 } 406 } 407 408 412 public void finalize() 413 { 414 this.destroy(); 415 } 416 } 417 | Popular Tags |