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 private static Scheduler singleton;
14 public static abstract class Task { public abstract void perform() throws Exception; }
15 public static void add(Task t) { singleton.runnable.append(t); }
16 public static void init() {
17 if (singleton != null) return;
18 singleton = Platform.getScheduler();
23 // API which must be supported by subclasses /////////////////////////////////////
26 * INVARIANT: all scheduler implementations MUST invoke Surface.renderAll() after performing a Task if no tasks remain
27 * in the queue. A scheduler may choose to invoke Surface.renderAll() more often than that if it so chooses.
29 public void run() { defaultRun(); }
30 protected Scheduler() { }
33 // Default Implementation //////////////////////////////////////////////////////
35 protected static Queue runnable = new Queue(50);
36 public void defaultRun() {
38 Task t = (Task)runnable.remove(true);
41 // FEATURE: be smarter about this
44 Log.log(Scheduler.class, e.toString());
45 } catch (Exception e) {
46 Log.log(Scheduler.class, e);