Reputation: 1479
I'm trying to use Timer.schedule()
to schedule an operation to happen 1s after. The problem I'm having is that after the operation is executed, there's an open thread left hanging and I'm forced to Ctrl+C to close it.
Am I doing something wrong? How do I stop timer from leaving an open thread?
public class MyClass {
public static int WAITING_WINDOW = 1000;
private boolean waiting = true;
public MyClass() {}
public void myMethod() {
new Timer().schedule(
new TimerTask() {
@Override
public void run() {
setWaiting(false);
}
}, WAITING_WINDOW
);
}
public void setWaiting(boolean waiting) {
this.waiting = waiting;
}
}
Upvotes: 0
Views: 199
Reputation: 73578
Keep a reference to the Timer
and call Timer.cancel()
.
And read the javadocs, it's all documented there!
Upvotes: 3