3 import java.awt.Toolkit;
4 import java.awt.event.InvocationEvent;
6 //import javajs.J2SRequireImport;
7 import javajs.api.JSFunction;
11 * An abstract class that takes care of simple threading in Java or JavaScript.
13 * To use it, subclass it and complete the necessary methods.
16 * There are three states: INIT, LOOP, and DONE.
18 * These states are passed into run1
24 //@J2SRequireImport(swingjs.JSToolkit.class)
25 public abstract class JSThread extends Thread implements JSFunction {
27 public static final int INIT = 0;
28 public static final int LOOP = 1;
29 public static final int DONE = 2;
31 public static int threadCount = 0;
33 protected boolean isJS;
36 this(null, "JSThread-" + (++threadCount));
39 public JSThread(String name) {
43 public JSThread(ThreadGroup group, String name) {
59 public synchronized void start() {
65 * Clazz.load("swingjs.JSToolkit").dispatch$O$I$I(this, 1, 0);
75 * thread initialization
77 * @return false to exit thread before any looping
79 protected abstract boolean myInit();
82 * check for continuing to loop
84 * @return true if we are to continue looping
86 protected abstract boolean isLooping();
90 * @return false to handle sleepAndReturn yourself
92 protected abstract boolean myLoop();
94 * what to do when the DONE state is reached
97 protected abstract void whenDone();
101 * @return the sleep time in milliseconds
103 protected abstract int getDelayMillis();
106 * handle an exception -- state will be set to DONE no matter what you do here
110 protected abstract void onException(Exception e);
113 * anything you want done in try{}catch(}finally().
114 * Note that this method is not fired if we are in JavaScript
115 * mode and the normal return from sleepAndReturn() is taken.
118 protected abstract void doFinally();
121 * a generic method that loops until done, either in Java or JavaScript.
123 * In JavaScript it will reenter and continue at the appropriate spot.
125 * This method may be overridden if desired.
127 * @see org.uwi.SimThread
131 protected void run1(int state) {
132 boolean executeFinally = true;
133 // called by thisThread.run();
135 while (!interrupted()) {
144 // to stop looping, return false from isLooping()
149 // To handle sleepAndReturn yourself, or to skip the
150 // sleep when desired, return false from myLoop();
151 // Note that doFinally must not be executed in this case.
152 // This is because JavaScript will do a return here
153 // for every loop, and Java will not.
154 if (myLoop() && sleepAndReturn(getDelayMillis(), state)) {
155 executeFinally = false;
165 } catch (Exception e) {
179 * @return true if we should interrupt (i.e. JavaScript)
180 * @throws InterruptedException
182 protected boolean sleepAndReturn(final int delay, final int state)
183 throws InterruptedException {
189 // in JavaScript, we need to do this through the system event queue,
190 // which in JSToolkit takes care of all the "thread" handling.
192 final JSThread me = this;
193 Runnable r = new Runnable() {
203 * function() {java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent$java_awt_AWTEvent(Clazz.new(java.awt.event.InvocationEvent.c$$O$Runnable,[me, r]))},
208 Toolkit.getDefaultToolkit().getSystemEventQueue()
209 .postEvent(new InvocationEvent(me, r));