I use the code below to increase a counter every 1 second, but if the application is running a long time, the counter is going to infinite. What can I do?
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
time ++;
ObservableObject.getInstance().updateValue(new TimerPojo(time,true));
}
},0,1000);
Related
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 am writing a script that will cause the players HP to decrease at an interval as long as they are within a range of the item/monster/lava whatever it is. I have the detection just fine, but I cant seem to get the interval to run. I know this is probably because I am creating a new TimerTask as I render, but I cant seem to figure it out.
for(Monster monster : monsters) {
renderer.processEntity(monster);
if(player.withinDistance(10, monster.getPosition()))
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
System.out.println("tick");
Player.PLAYER_HEALTH -= 10;
}
}, 2000, 2000);
}
So for all monsters, it checks the positions, if you are near it opens a timer task that should tick every 2 seconds while that condition is true. How can I make this work properly? Is a timer task optimal for this situation?
The problem here is that you are updating the PLAYER_HEALTH static property of the Player class. You should update the Player instance!
for(Monster monster : monsters) {
renderer.processEntity(monster);
if(player.withinDistance(10, monster.getPosition()))
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
System.out.println("tick");
player.decreaseHealth(10); // Use the instance
}
}, 2000, 2000);
}
Also, if your program ends, the TimerTask will end too. Make sure your program is still running.
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.
i wan't a timer do a job every 2.5 seconds (at the start of the program),
that is working with the following code.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
// #Override
#Override
public void run() {
here is the code, and i do Speed = Speed-500
}, Speed,Speed);
Speed is a int:
public int Speed=2500;
buth the problem is that the speed of the timer stays on the 2500, while the variable speed lowers each time with 500, so that part is working. Only the timer doesn't check if Speed has changed.
Can somebody help me with this?
you cant do that because it will fix that with Timer once you done the schedule.
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
In this case you can cancel the previous one and schedule new TimerTask.
Timer timer = new Timer();
initialize the speed here
loop based on time
timer.schedule(new TimerTask() {
// #Override
#Override
public void run() {
here is the code, and i do Speed = Speed-500
}, Speed,Speed);
I need to have my code executed every short period of time, for example every 100ms (or even less).
I guess I can spawn a thread and add an infinite loop inside it, but I don't think this is good.
You can use Timer and TimerTask.
final Timer timer = new Timer();
final int period = 100;
final int delay = 0;
timer.schedule(new TimerTask()
{
#Override
public void run()
{
log.debug("executing something");
}
}, delay, period);