Late addition to this thread....
Based on the example I put together the following
servlet thread
test..
I was curious and commented out the 'interrupted = true' flag setting as you can see...but...it STILL stops the thread....why is that?
import java.lang.Thread;
public class ConnCheck extends Thread {
private boolean interrupted = false;
private int interval;
public ConnCheck(int interval) {
this.interval = interval;
}
public void run() {
setPriority(Thread.MIN_PRIORITY);
try {
while (true) {
if(isInterrupted()) break;
Thread.sleep(interval);
// ..do stuff....
}
}
catch(java.lang.InterruptedException e) {}
}
// !!! This still stops the thread even with this comment!
synchronized public void stopMe(){
// interrupted = true;
interrupt();
}
synchronized public boolean isInterrupted() {
return interrupted;
}
}
I call the stopMe() function from another servlet which gets a handle to the connCheck servlet from servletContext.
i.e.
ConnCheck d = (ConnCheck)getServletContext().getAttribute("conncheck");
d.stopMe();
It stops it...but why?
Thanks