2002/05/28 21:30:03
[org.ibex.core.git] / src / org / xwt / ThreadMessage.java
index 558c49d..cd22019 100644 (file)
@@ -50,6 +50,30 @@ public class ThreadMessage extends Thread implements Message {
         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 {