private static Object[] emptyobj = new Object[] { };
/** creates a new thread to execute function <tt>f</tt> */
- public static void newthread(Function f) {
+ public static synchronized void newthread(Function f) {
ThreadMessage ret = (ThreadMessage)spare.remove(false);
if (ret == null) {
if (threadcount < Platform.maxThreads()) ret = new ThreadMessage();
ret.f = f;
MessageQueue.add(ret);
}
+
+ /** attempts to put this thread into the background to perform a blocking operation; returns false if unable to do so */
+ public static boolean suspendThread() {
+ // put ourselves in the background
+ Thread thread = Thread.currentThread();
+ if (!(thread instanceof ThreadMessage)) {
+ Context cx = Context.enter();
+ if (Log.on) Log.log(ThreadMessage.class, "attempt to perform background-only operation in a foreground thread at " +
+ cx.interpreterSourceFile + ":" + cx.interpreterLine);
+ return false;
+ }
+ ThreadMessage mythread = (ThreadMessage)thread;
+ mythread.setPriority(Thread.MIN_PRIORITY);
+ mythread.done.release();
+ return true;
+ }
+
+ /** re-enqueues this thread */
+ public static void resumeThread() {
+ ThreadMessage mythread = (ThreadMessage)Thread.currentThread();
+ MessageQueue.add(mythread);
+ mythread.setPriority(Thread.NORM_PRIORITY);
+ mythread.go.block();
+ }
public void run() {
try {