Thanks for the opinions
If implement Runnable, and you call "sleep()" within run(). After
you call interrupt(), then thread won't stop too.
from Peter
It's not clear whether you want the thread to stop or not.
Anyway, here's an example where the thread doesn't stop when interrupted.
Uncomment the line in the loop to make it stop when it is interrupted.
Steve
/** QuickTest.java */
public class QuickTest implements Runnable {
private volatile boolean shouldRun = false;
public static void main(String[] args) {
// Create and start a QuickTest
QuickTest qt = new QuickTest();
Thread t = new Thread(qt);
t.start();
// wait 5 seconds
try {
Thread.sleep(5000);
} catch(InterruptedException ie) {
System.out.println("Main thread interrupted - shouldn't happen");
}
// interrupt the test thread
t.interrupt();
// wait another 5 seconds
try {
Thread.sleep(5000);
} catch(InterruptedException ie) {
System.out.println("Main thread interrupted - shouldn't happen");
}
}
/** Loop forever. */
public void run() {
shouldRun = true;
while(shouldRun) {
System.out.println("Snooze...");
try {
Thread.sleep(1000);
} catch(InterruptedException ie) {
System.out.println("Sleeper loop was interrupted.");
shouldRun = false;
}
}
System.out.println("Sleeper loop terminating");
}
}
//=============================================================