I have one button include timer, but when I clicked the button again, same timer start counting twice. I want to stop the counter every time and restart it. how can I do it?
#FXML
private void handleButtonAction(ActionEvent event) {
int cnt=0;
Timer timer=new Timer();
TimerTask task=new TimerTask() {
#Override
public void run() {
cnt++;
System.out.println("task: "+cnt);
}
};
timer.schedule(task, 0, 3000);
}
when I clicked the button again, it calls system out twice.
By calling Timer timer = new Timer(); inside handleButtonAction you create a new Timer with every click.
You have to declare the Timer outside the method, then you can restart it by clicking the button like:
Timer timer;
private void handleButtonAction(ActionEvent event) {
int cnt=0;
timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
cnt++;
System.out.println("task: "+cnt);
}
};
timer.schedule(task, 0, 3000);
}
This way you always restart the same timer.
Related
I'm tyring to make a button that starts method with simple timer that count from 5 to 0 and that's it.
But my problem is when i click the button multiple times the method speeds up the timer and break it.
In the final form this button must every time when it is clicked should reset the countdown.
public class Buttons extends TimerTask {
int delay = 1000;
int period = 1000;
static Timer timer;
static int interval =10;
public static void setTimer(Label label) {
System.out.println("timer start");
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if(interval > 0)
{
Platform.runLater(() -> label.setText("TIME TO OVERHEAT: "+interval));
System.out.println(interval);
interval--;
}
else
timer.cancel();
}
}, 1000,1000);
}
I am using java.util.Timer class and I am using its schedule method to perform some task, but after executing it for 6 times I have to stop its task.
How should I do that?
Keep a reference to the timer somewhere, and use:
timer.cancel();
timer.purge();
to stop whatever it's doing. You could put this code inside the task you're performing with a static int to count the number of times you've gone around, e.g.
private static int count = 0;
public static void run() {
count++;
if (count >= 6) {
timer.cancel();
timer.purge();
return;
}
... perform task here ....
}
Either call cancel() on the Timer if that's all it's doing, or cancel() on the TimerTask if the timer itself has other tasks which you wish to continue.
You should stop the task that you have scheduled on the timer:
Your timer:
Timer t = new Timer();
TimerTask tt = new TimerTask() {
#Override
public void run() {
//do something
};
};
t.schedule(tt,1000,1000);
In order to stop:
tt.cancel();
t.cancel(); //In order to gracefully terminate the timer thread
Notice that just cancelling the timer will not terminate ongoing timertasks.
Terminate the Timer once after awake at a specific time in milliseconds.
Timer t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
System.out.println(" Run spcific task at given time.");
t.cancel();
}
}, 10000);
I have an application in Java where I need to schedule a TimerTaskwhich will be executed after 500ms , however if a certain event occurs, I must reset the timer for this task (so that we must wait another 500ms for it to execute). I have a timer declared for the whole class. I use the following code:
public static void main(String[] args) {
if (curr_pck == my_pck) {
timer.cancel();
timer.purge();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
myTask();
}
}, 500);
}
}
public static void myTask() {
timer.cancel();
timer.purge();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
myTask();
}
}, 500);
//EXECUTE CODE WHICH ISN'T RELEVANT TO THE QUESTION
}
I know that if I use timer.cancel() I can't reuse the timer object, however I thought reinitialising it in the line timer = new Timer() should solve this issue. Is there any way around this?
EXCEPTION on line timer.schedule(new TimerTask() { inside myTask() function:
java.lang.IllegalStateException: Timer already cancelled.
Create a class Timerr with the appropriate methods. Then access it as if it were a normal timer.
public class Timerr
{
private Timer timer;
public Timerr()
{
timer = new Timer();
start();
}
public void start()
{
timer.schedule(new TimerTask()
{
#Override
public void run()
{
System.out.println("hi");
}
}, 500);
}
public void reset()
{
timer.cancel();
timer.purge();
start();
}
}
Create instance
private Timerr timer = new Timerr();
Do your reset
if(condition)
{
timerr.reset();
}
You may want to check out Java's Swing timer. It works somewhat differently and you may have to write an internal class or an actionlistener, but the Swing timer includes .stop() and .restart(), which seem like they would work better in your application.
I am working on a board game, and in a part of the game, I need to have like this button which is when clicked, will change a label repeatedly counting 1 to 5 with 1 second delay interval, and after will change the label into "done", but the problem it changes the label into "done" first then the counting.
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
// #Override
public void run() {
count++;
if (count >= 6) {
timer.cancel();
timer.purge();
return;
}
lbl.setText(String.valueOf(count));
}
}, 1000,1000);
lbl.setText("done");
}});
When you click on the button, the actionPerformed() method is executed sequentially:
Create a new timer
Register a task to execute regularly each second, in the future
Set the button label to "Done"
Then, later, the timer do its job and will start to increase the variable count, updating each time the button label. Reading like this should help you to understand what is happening: the timer is executed in a separate thread. timer.scheduleAtFixedRate() is a non-blocking function, which registers a TimerTask to execute later, and returns immediately.
To fix your issue, something like that could be a solution:
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
// #Override
public void run() {
count++;
if (count >= 6) {
timer.cancel();
timer.purge();
// We set the label to done only when the counter
// reaches the value 6, after button displayed 5
lbl.setText("done");
return;
}
lbl.setText(String.valueOf(count));
}
}, 1000,1000);
}});
I am implementing a timer:
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//Do something;
}
},1000,1000);
But, I would like to have a timeout so that after lets say 100 tries/ 10 seconds, the timer stops automatically.
Thanks.
try
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int n = 0;
#Override
public void run() {
System.out.println(n);
if (++n == 5) {
timer.cancel();
}
}
},1000,1000);
You can simply have a variable outside the run method that keeps count of the iteration. Make an if statement inside the run() method that cancels the timer when it hits your desired amount. Increase the variable by one everytime the run() method executes.
start another timer, as soon as above timer starts, which cancels the above timer after 10sec. check to code below as a quick solution. but better you cancel the task() instead of timer.
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
timer2.schedule(new TimerTask() {
#Override
public void run() {
timer1.cancel();
}
},0);
//Do something;
}
},1000,1000);
timer2 = new Timer();
I dont think we have java API for this in Timer class. You need to do it programmatically by implementing some custom logic based on your requirement.