1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** Implements cooperative multitasking */
9 public class Scheduler {
11 // Public API Exposed to org.xwt /////////////////////////////////////////////////
13 public static abstract class Task { public abstract void perform(); }
14 public static void add(Task t) { singleton.runnable.append(t); }
15 public static void init() { singleton.run(); }
18 // API which must be supported by subclasses /////////////////////////////////////
20 protected Scheduler() { }
23 * INVARIANT: all scheduler implementations MUST invoke
24 * Surface.renderAll() after performing a Task if no tasks remain
25 * in the queue. A scheduler may choose to invoke
26 * Surface.renderAll() more often than that if it so chooses.
28 public void run() { defaultRun(); }
31 // Default Implementation /////////////////////////////////////
33 protected static Queue runnable = new Queue(50);
34 private static final Scheduler singleton = Platform.getScheduler();
35 public void defaultRun() {
37 Task t = (Task)runnable.remove(true);
40 // FIXME: be smarter about this
42 } catch (Exception e) {
43 Log.log(Scheduler.class, "Task threw an exception: " + e);
44 Log.log(Scheduler.class, e);