============================================================================== | A P P E N D I X 2 | ============================================================================== //{{{ stamp Date: Tue, 14 May 96 12:48:17 BST From: phw Message-Id: <9605141148.AA07716@mint.ukc.ac.uk> To: occam-com@ukc.ac.uk Subject: Java technical question? //}}} Can anyone help me with the following Java problem ... or point me at some Java hot-line that answers technical questions? //{{{ my problem What I want to do is have one thread detect if another thread has been suspended (... the first thread's task is to resume the second one, but it musn't do so before the other has really suspended!). //}}} //{{{ what Sun says According to Sun's "Java Tutorial": > IllegalThreadStateException > > The runtime system throws an IllegalThreadStateException > when you call a method on a thread and that thread's state > does not allow for that method call. For example, > IllegalThreadStateException is thrown when you call > suspend() on a thread that is not "Runnable". //}}} //{{{ so why doesn't this work? What I really need is to attempt to resume the other thread but get an IllegalThreadStateException if the other thread were still "Runnable" (i.e. not yet suspended). Then, I could loop doing this until the resume actually succeded. But it didn't work! So, I tried to suspend a thread that was definitly already suspended (which surely counts as not "Runnable" in the above extract from Sun's Java tutorial) and see if IllegalThreadStateException is raised. But it isn't! See: //{{{ ThingTest.java import java.io.*; import java.util.*; import java.lang.*; //{{{ class Thing extends Thread class Thing extends Thread { protected Thread me; //{{{ public Thing () public Thing () { setName ("Thing"); start (); } //}}} //{{{ public void run () { public void run () { me = Thread.currentThread(); System.out.println (" " + getName() + " " + toString()); System.out.println ("(timestamp = " + System.currentTimeMillis() + ")" + " Thing: suspending ..."); me.suspend (); System.out.println ("(timestamp = " + System.currentTimeMillis() + ")" + " Thing: resumed ..."); } //}}} } //}}} //{{{ class ThingTest class ThingTest { //{{{ public static void main (String argv []) public static void main (String argv []) { Thing thing = new Thing (); //{{{ Thread.sleep (5000); try { Thread.sleep (5000); } catch (InterruptedException e) { } //}}} //{{{ attempt to suspend thing (which should already be suspended) try { thing.me.suspend (); System.out.println ("(timestamp = " + System.currentTimeMillis() + ")" + " WRONG: suspended an already suspended thing ..."); } catch (IllegalThreadStateException e) { System.out.println ("(timestamp = " + System.currentTimeMillis() + ")" + " RIGHT: attempt to suspend suspended thing failed."); } //}}} thing.me.resume (); //{{{ wait for thing to terminate try { thing.join (); } catch (InterruptedException ignored) { } //}}} } //}}} } //}}} //}}} When run, we get: //{{{ java ThingTest mint.ukc.ac.uk% javac ThingTest.java mint.ukc.ac.uk% java ThingTest Thing Thread[Thing,5,main] (timestamp = 832073730469) Thing: suspending ... (timestamp = 832073735513) WRONG: suspended an already suspended thing ... (timestamp = 832073735526) Thing: resumed ... mint.ukc.ac.uk% //}}} which ain't right! //}}} Have I misunderstood something or does javac/java get it wrong? Cheers, Peter.