1 // Copyright 2007 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
9 // FEATURE: auto-rescheduling
10 // (currently cronjobs must manually reschedule themselves)
13 * A manager for periodic tasks ("cron jobs")
15 public final class Cron extends ImmortalThread {
17 private static ThreadPool threadPool;
19 private Vec tasks = new Vec();
20 private long wakeup = 0;
22 public Cron(ThreadPool threadPool) {
24 this.threadPool = threadPool;
28 public synchronized void cycle() {
30 long now = System.currentTimeMillis();
31 if (wakeup == 0) wait();
32 else if (wakeup > now) wait(wakeup - now);
33 } catch (Exception e) { Log.error(this, e); }
35 long now = System.currentTimeMillis();
36 // FIXME: linear scan is inefficient
37 for(int i=0; i<tasks.size(); i++) {
38 Task t = (Task)tasks.get(i);
41 threadPool.start(t.runnable);
43 if (wakeup == 0 || wakeup > t.when)
49 public void executeLater(long howLongFromNow, Runnable runnable) {
50 new Task(runnable, System.currentTimeMillis()+howLongFromNow);
54 final Runnable runnable;
56 public Task(Runnable runnable, long when) {
57 this.runnable = runnable;
59 synchronized(Cron.this) {
61 if (wakeup == 0 || wakeup > when) {