[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Java solution of checking suspended threads !
phw wrote:
>
> //{{{}}}
>
> 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!).
>
> etc.
The suspend method calls a checkAccess method which determines if the currently running thread has
persmission to modify this thread. Only the checkAccess method throws the exception
"ServiceException". To check if a thread is suspended is not implemented.
Here I wrote a program, based on the program of Peter Welch, where an isSuspended() method returns
true is the thread is suspended. To do this the original suspend() and resume() methods are extended
with the condition variable "suspendflag". Overriding suspend() and resume() is not possible because
they are defined as final. So I wrote the methods Suspend() and Resume().
/**
* ThingTest.java
**/
import java.io.*;
import java.util.*;
import java.lang.*;
class Thing extends Thread
{
private boolean suspendflag = false;
public Thing ()
{
setName ("Thing");
start ();
}
public void run ()
{
System.out.println (" " + getName() + " " + toString());
System.out.println ("(timestamp = " + System.currentTimeMillis() + ")" +
" Thing: suspending ...");
this.Suspend();
System.out.println ("(timestamp = " + System.currentTimeMillis() + ")" +
" Thing: resumed ...");
}
public void Suspend()
{
suspendflag = true;
super.suspend();
}
public void Resume()
{
suspendflag = false;
super.resume();
}
public boolean isSuspended()
{
return suspendflag;
}
}
class ThingTest {
public static void main (String argv []) {
Thing thing = new Thing ();
try {
Thread.sleep (5000);
} catch (InterruptedException e) { }
if (thing.isSuspended())
System.out.println("(1) Suspended !");
else
System.out.println("(1) NOT suspended !");
thing.Resume ();
if (thing.isSuspended())
System.out.println("(2) Suspended !");
else
System.out.println("(2) NOT suspended !");
try
{
thing.join ();
}
catch (InterruptedException ignored) {}
}
}
Regards,
Gerald Hilderink.