Yes, I got answers for you.
I'm sorry to tell you the following but you should mostly ignore the
answers you got. These techniques are not professionnal. Period.
1st, you sure will have a boolean to change/check somewhere, but
the volatile keyword (that supposably protects threads from processor
caching) is not implemented in many jvm. You may easily confirm that hot
topic with a simle search on the web.
2nd, the only sure way to do "messaging" between threads required the use
of a "synchronized" zone, that forces the thread fo resync with memory.
This is critical in multiprocessors platforms.
3rd, your thread to be paused need to cooperate, i.e. it must be ready to
be notified or capable to handle interruptions (only one of the 2
technique is enough, and I suggest you go with notifiations, it is less
work, although you might want to combine both)
-----------
package tests;
import java.io.InterruptedIOException;
public abstract class PausableLoopThread extends Thread {
long delay;
//critical zone 1:-----
final Object lock = new Object();
boolean paused = false;
boolean running = false;
//---------------------
boolean requestedInterrupt = false;
public PausableLoopThread(String name, long delay) {
this(null, name, delay);
}
public PausableLoopThread(ThreadGroup group, String name, long
delay) {
super(group, name);
if(delay<=0)
throw new IllegalArgumentException("delay <= 0");
this.delay = delay;
}
public final void interrupt() {
requestedInterrupt = true;
super.interrupt();
}
public final void run() {
synchronized(lock) {
running = true;
}
while(true) {
boolean _p;
synchronized(lock) {
if(!running)
break;
_p = paused;
} //unlock zone to process loopImpl, otherwise
changePause() would block while loopImpl() running (may be long)
if(!_p) {
try {
loopImpl();
} catch(InterruptedException e) {
if(!requestedInterrupt)
System.err.println("Thread was
interrupted by another unexpected way!");
break;
} catch(InterruptedIOException e) {
if(!requestedInterrupt)
System.err.println("Thread was
interrupted by another unexpected way!");
break;
} catch(Exception e) {
if(requestedInterrupt) { //detect stupid 3rd
party that do not rethrow Interrupts
System.err.println("Thread was
interrupted but didn't throw InterruptedException: "+e);
//e.printStackTrace();
break;
} else {
e.printStackTrace();
}
}
}
synchronized(lock) {
try {
lock.wait(paused ? 0 : delay); //gets out by
timeout of by notify
} catch(InterruptedException e) {
if(!requestedInterrupt)
System.err.println("Thread was
interrupted by another unexpected way!");
break;
}
}
}
}
protected abstract void loopImpl() throws InterruptedException,
InterruptedIOException;
public final void requestPause() {
changePause(true);
}
public final void requestResume() {
changePause(false);
}
private void changePause(boolean pause) {
synchronized(lock) {
boolean b = this.paused;
this.paused = pause;
//if(!pause && b!=pause) //notify only if resumed, why
waking up a waiting thread!
lock.notify();//kick out of wait
}
}
public final void requestStop() {
synchronized(lock) {
this.running = false;
lock.notify();//kick out of wait, if any
}
}
//========
public static void main(String[] args) throws Exception{
PausableLoopThread p = new PausableLoopThread("pt", 300) {
final long t0 = System.currentTimeMillis();
protected void loopImpl() throws InterruptedException,
InterruptedIOException {
System.out.println("loop it! "+
(System.currentTimeMillis()-t0));
}
};
p.start();
Thread.sleep(4000);
p.requestPause();
Thread.sleep(4000);
p.requestResume();
Thread.sleep(4000);
p.requestStop();
//p.interrupt();
Thread.sleep(4000);
}
}
(e-mail address removed) (Bryan R. Meyer) wrote in
Hello Everyone,
Hopefully, someone has some pointers for my problem. Suppose I have
an applet which starts a new thread A. The applet implements
MouseListener and when the user puts the mouse over the applet, a
MouseEvent occurs. When a user generates this MouseEntered event,
thread A is to be paused. Unfortunately, the currently executing
thread is AWT-EventQueue. How can I pass that event to thread A so
that I can pause the thread?
Thanks for any suggestions. I haven't been able to find much on this
subject.
Bryan