1 26 27 28 package org.objectweb.jonathan.libs.resources; 29 30 import org.objectweb.jonathan.apis.kernel.Context; 31 import org.objectweb.jonathan.apis.kernel.ContextFactory; 32 import org.objectweb.jonathan.apis.kernel.InternalException; 33 import org.objectweb.jonathan.apis.resources.Scheduler; 34 import org.objectweb.jonathan.apis.resources.Job; 35 36 import org.objectweb.util.monolog.api.BasicLevel; 37 38 50 public class JScheduler implements Scheduler { 51 52 static int sched_nb = 0; 53 54 String sched_name; 55 int id = 0; 56 57 58 int max_waiting; 59 60 69 public boolean verbose; 70 71 72 JJob[] pool; 73 74 75 int waiting; 76 77 protected ContextFactory context_factory; 78 79 83 public JScheduler(ContextFactory context_factory) { 84 this(5,false,context_factory); 85 } 86 87 94 public JScheduler(int max_waiting,boolean verbose, 95 ContextFactory context_factory) { 96 this.max_waiting = max_waiting; 97 this.verbose = verbose; 98 this.context_factory = context_factory; 99 sched_name = "p" + (sched_nb++) + "."; 100 pool = new JJob[max_waiting]; 101 waiting = 0; 102 } 103 104 108 public synchronized Job newJob() { 109 JJob job = null; 110 if (waiting == 0) { 111 job = new JJob(sched_name + (id++)); 114 } else { 115 job = pool[--waiting]; 116 } 117 return job; 118 } 119 120 124 public Job getCurrent() { 125 if (Thread.currentThread() instanceof Job) 126 return (Job) Thread.currentThread(); 127 else 128 return new KJob(); 129 } 130 131 138 public void yield() { 139 Thread.yield(); 140 } 141 142 153 public void wait(Object lock) throws InterruptedException { 154 lock.wait(); 155 } 156 157 169 public void wait(Object lock,long millis) throws InterruptedException { 170 lock.wait(millis); 171 } 172 173 181 public void notify(Object lock) { 182 lock.notify(); 183 } 184 185 193 public void notifyAll(Object lock) { 194 lock.notifyAll(); 195 } 196 197 204 public void escape() {} 205 206 212 public void enter() {} 213 214 boolean register(JJob job) { 215 if (waiting < pool.length) { 216 pool[waiting++] = job; 217 return true; 218 } else { 219 return false; 220 } 221 } 222 223 class JJob extends Thread implements Job { 224 Runnable task; 225 Context context; 226 227 JJob(String name) { 228 super(name); 229 setDaemon(true); 230 task = null; 231 context = null; 232 super.start(); 233 } 234 235 236 public final void run() { 237 try { 238 synchronized (this) { 239 while (task == null) { 240 this.wait(); 241 } 242 } 243 while (true) { 244 task.run(); 246 if (context != null) { 247 context.release(); 248 context = null; 249 } 250 synchronized(JScheduler.this) { 252 if (register(this)) { 253 task = null; 254 } else { 255 return; 256 } 257 } 258 if (task == null) { 259 synchronized (this) { 260 while (task == null) { 261 this.wait(); 262 } 263 } 264 } 265 } 266 } catch (InterruptedException e) {} 267 } 268 269 public synchronized void run(Runnable message) { 270 if (task != null) { 271 throw new InternalException("Can't use this job more than once"); 272 } else { 273 task = message; 274 this.notify(); 275 } 276 } 277 278 public Context getContext() { 279 if (context == null) { 280 context = context_factory.newContext(); 281 } 282 return context; 283 } 284 285 public String toString() { 286 return getName(); 287 } 288 } 289 290 private static ThreadLocal threadLocalContext = new ThreadLocal (); 291 292 305 class KJob implements Job { 306 307 public KJob() { 308 } 309 310 314 public void run(Runnable m) { 315 if ((LoggerProvider.logger != null) 318 &&(LoggerProvider.logger.isLoggable(BasicLevel.WARN))) { 319 LoggerProvider.logger.log(BasicLevel.WARN,"KJob being asked to run a new task... Alert!"); 320 } 321 } 325 326 public Context getContext() { 327 if (threadLocalContext.get() == null) { 328 threadLocalContext.set(context_factory.newContext()); 329 } 330 331 return (Context)threadLocalContext.get(); 332 } 333 } 334 335 336 337 } 338 339 340 | Popular Tags |