1 22 package org.jboss.mx.util; 23 24 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 25 import org.jboss.util.timeout.Timeout; 26 27 42 public abstract class SchedulableRunnable 43 implements Runnable 44 { 45 47 50 private SynchronizedLong nextRun = new SynchronizedLong(0); 51 52 55 private RunnableScheduler scheduler; 56 57 60 private boolean running; 61 62 65 private boolean reschedule; 66 67 70 private Timeout timeout; 71 72 74 76 79 public SchedulableRunnable() 80 { 81 } 82 83 85 90 public long getNextRun() 91 { 92 return nextRun.get(); 93 } 94 95 103 public synchronized void setNextRun(long nextRun) 104 { 105 if (timeout != null) 107 timeout.cancel(); 108 109 this.nextRun.set(nextRun); 111 112 if (running == false && scheduler != null) 115 { 116 timeout = scheduler.add(this); 118 } 119 else 120 { 121 reschedule = true; 123 } 124 } 125 126 132 public synchronized RunnableScheduler setScheduler(RunnableScheduler scheduler) 133 { 134 if (this.scheduler == scheduler) 136 return this.scheduler; 137 138 RunnableScheduler result = this.scheduler; 140 141 if (this.timeout != null) 143 timeout.cancel(); 144 145 this.scheduler = scheduler; 147 148 if (scheduler == null) 150 reschedule = false; 151 152 else if (running == false) 155 timeout = scheduler.add(this); 156 else 157 reschedule = true; 158 159 return result; 161 } 162 163 166 public abstract void doRun(); 167 168 170 176 public final void run() 177 { 178 startRun(); 179 try 180 { 181 doRun(); 182 } 183 finally 184 { 185 endRun(); 186 } 187 } 188 189 191 193 195 197 200 private synchronized void startRun() 201 { 202 running = true; 203 } 204 205 208 private synchronized void endRun() 209 { 210 running = false; 211 if (reschedule == true) 212 timeout = scheduler.add(this); 213 reschedule = false; 214 } 215 216 } 218 219 | Popular Tags |