Reputation: 155
I have a function that when I click a button it starts a thread and what I want it to do is when I click another button it will stop the thread. The thread timer that I am using looks like this.
new java.util.Timer().scheduleAtFixedRate(new java.util.TimerTask() {
@Override
public void run() {
//Blah Blah Blah
}
}, 20, 5000);
Upvotes: 0
Views: 1880
Reputation: 80
// call the method cancel to stop it
java.util.Timer time = new java.util.Timer();
time.scheduleAtFixedRate(new java.util.TimerTask() {
@Override
public void run() {
//Blah Blah Blah
}
}, 20, 5000);
time.cancel();
Upvotes: 1
Reputation: 1
Why do you even need a Timer for this task? Timers are meant for scheduling thread execution in the future or at specific intervals.
Based on your task description, all you need to do is listen for the button presses, and start/stop the thread when triggered.
Upvotes: 0