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 {
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 = /** @j2sNative true || */false;
36 this(null, "JSThread-" + (++threadCount));
39 public JSThread(String name) {
43 public JSThread(ThreadGroup group, String name) {
53 public synchronized void start() {
59 * Clazz.load("swingjs.JSToolkit").dispatch$O$I$I(this, 1, 0);
69 * thread initialization
71 * @return false to exit thread before any looping
73 protected abstract boolean myInit();
76 * check for continuing to loop
78 * @return true if we are to continue looping
80 protected abstract boolean isLooping();
84 * @return false to handle sleepAndReturn yourself
86 protected abstract boolean myLoop();
88 * what to do when the DONE state is reached
91 protected abstract void whenDone();
95 * @return the sleep time in milliseconds
97 protected abstract int getDelayMillis();
100 * handle an exception -- state will be set to DONE no matter what you do here
104 protected abstract void onException(Exception e);
107 * anything you want done in try{}catch(}finally().
108 * Note that this method is not fired if we are in JavaScript
109 * mode and the normal return from sleepAndReturn() is taken.
112 protected abstract void doFinally();
115 * a generic method that loops until done, either in Java or JavaScript.
117 * In JavaScript it will reenter and continue at the appropriate spot.
119 * This method may be overridden if desired.
121 * @see org.uwi.SimThread
125 protected void run1(int state) {
126 boolean executeFinally = true;
127 // called by thisThread.run();
129 while (!interrupted()) {
138 // to stop looping, return false from isLooping()
143 // To handle sleepAndReturn yourself, or to skip the
144 // sleep when desired, return false from myLoop();
145 // Note that doFinally must not be executed in this case.
146 // This is because JavaScript will do a return here
147 // for every loop, and Java will not.
148 if (myLoop() && sleepAndReturn(getDelayMillis(), state)) {
149 executeFinally = false;
159 } catch (Exception e) {
173 * @return true if we should interrupt (i.e. JavaScript)
174 * @throws InterruptedException
176 protected boolean sleepAndReturn(final int delay, final int state)
177 throws InterruptedException {
183 // in JavaScript, we need to do this through the system event queue,
184 // which in JSToolkit takes care of all the "thread" handling.
186 final JSThread me = this;
187 Runnable r = new Runnable() {
198 * java.awt.Toolkit.getDefaultToolkit$().getSystemEventQueue$().postEvent$java_awt_AWTEvent(
199 * Clazz.new_(java.awt.event.InvocationEvent.c$$O$Runnable,[me, r]))},
204 Toolkit.getDefaultToolkit().getSystemEventQueue()
205 .postEvent(new InvocationEvent(me, r));