Java Timer going off every hour Sat-Sun - java

I have a Timer class that I want to schedule to run for 30 minutes and for it to execute every hour from Saturday at 12AM to Sunday at midnight.
How would I go about doing this?
Thanks.

The java.util.Timer class does not have the functionality to do this using repeated tasks:
You cannot configure a Timer to >>stop<< running repeated tasks at a given time
The execution model for repeated tasks is "fixed-delay execution"; i.e. a new task is scheduled based on the end-point of the previous task. That results in time slippage ... if tasks take a long time.
The simple solution is:
create a single Timer object
use schedule(TimerTask task, Date time) in a loop to schedule 48 separate tasks, starting on each hour in the range that you require.
According to the javadoc, Timer is scalable and should be able to cope with lots of scheduled tasks efficiently.
The above does not deal with the requirement that each task runs for 30 minutes. It is not clear what that actually means, but if you want the task to run for no more than 30 minutes, then you need to implement a "watch dog" that interrupts the thread running the task when its time is up. The task needs to be implemented to check for and/or handle thread interrupts appropriately.

You could have a loop run and in the loop just sleep for an hour or however long you wanted. Would something like this work?
for(int i = 0; i < 24; i++) {
// do whatever you want to do
try {
// Sleep for an hour
Thread.sleep(6000000);
catch(InterruptedException e) {
e.printStackTrace();
}
}
I might have misunderstood the question... but if that's what you wanted.. then... yay?

Related

Java Run Method every 30th second of a minute

I have the following while loop:
while (keepRunning) {
if (!once) {
// Run a test method every 30th second
// Run the new calculations from the database
new CalculatorDriver().run();
// Wait in the while loop for 1 second before looping again
TimeUnit.SECONDS.sleep(1);
}
}
This while loop will loop every second through the code, but now I want to run a method called: getNewCalculations() every 30th second of a minute, so for example, the method needs to run at:
18:26:30
18:27:30
18:28:30
I already found a way to run a method every x seconds:
Timer timer = new Timer();
timer.schedule(new Task(), 60 * 1000);
But I also need to start it at a specific point. In C# someone tried this to run a script every 30th and every 0th second: https://stackoverflow.com/a/53846390/10673107.
I could just add an if where the second can't be equal to 0, but I was wondering if there was a better way to do this.
How can I run it only in the 30th second?
Set an initial delay by calculating the number of seconds to wait until the next 30th second is due to arrive. If the current moment is :10, wait 20 seconds. If the current moment is :56, wait 34 seconds.
The Duration class may help with that, along with ZonedDateTime class.
If you read the Javadoc for Timer/TimerTask you will learn those classes were supplanted years ago by the Executors framework that arrived in Java 5.
Use a ScheduledExecutorService to schedule a task to run after an initial delay specified by you. After the initial delay, specify a repeating period of one minute.
Your task will then be repeating on the 30th second mark of every minute. Know that this scheduling is approximate. The host OS, the JVM’s internal scheduler, and garbage-collection all impact when the task actually runs. So each run may vary.
Or, if you want to protect against any politician-imposed anomalies on your region’s time-keeping, schedule only a one-time scheduled task rather than repeating task. Pass to that task a reference to the ScheduledExecutorService object. The task can then schedule its own next run after running the initial-delay calculation again.
Be sure to eventually shutdown your executor service. Otherwise its backing pool of threads may continue to run indefinitely even after your app ends, like a zombie 🧟‍♂️.
Be aware that any exception or error bubbling up to the scheduled executor service will silently halt any further scheduling.
All of these topics have been addressed many times already on Stack Overflow. Search to learn more.

Get remaining delay time of a Task in libgdx

How do I get the remaining delay time of a Task that has been scheduled?
Let's say I schedule my Task like this:
Timer.schedule(myTask, 10);
How Do I get the remaining delay time before the Task is scheduled without counting the time myself?
You have getExecuteTimeMillis which you can compare to System.currentTimeMillis.
Source

Timer triggered unexpectedly in java

I have started timer with fixed rate. When user change system Time at that time task is executed continuously.It doesn't consider about the period time. How to manage this.
timer.scheduleAtFixedRate(task, delay, period);
Note : Now system time is current time.Period is 30 seconds. Now user change system time into after 10 mins from current time.At that time timer task not consider about the period. Within a second it execute the task 20 times.
When I use
timer.schedule(task, delay, period);
instead of
timer.scheduleAtFixedRate(task, delay, period);
task working normally. If I change system time to 10 mins past time from now task doesn't executed...
How to solve this ? :(
Have a look at this Stack Overflow answer; long story short, using scheduleAtFixedRate will queue up the tasks you have scheduled and fire them relative to the system time that the task was first queued; if anything should delay these tasks from firing, they will play 'catch up' and fire as fast as they can. So in your example of how changing the system time to +10 minutes made the event fire 20 times make since given that your period is 30 seconds (1m/2 = 30s, 10m*2 = 20 events).
Using the schedule function has similar issues, except it does not play 'catch up'; if you had a timer set to fire off every 30 seconds, then change the system time to +10 minutes only 10 seconds after the timer has started, your first event will trigger, then continue to wait the 30 second delay and fire again (unless you change the system clock again).
If you want something that is independent of system time you'll need monotonic clock values. To my knowledge the Java API does not directly contain any monotonic timers (for numerous reasons), but that doesn't stop you from implementing one yourself (albeit at the cost of possible wasted execution time in custom timer class code). Here's a Stack Overflow question regarding this, as well as a Google Group discussion post on it.
If you're really hard pressed for using a monotonic timer you could potentially use the POSIX API's and do some Java->C JNI work.
I hope that can help (at least point in a solid direction).

Constant "ticks" per second thread Java

I know this may be completely obvious for more experenced programmers but I can't find anything on it.
I need to make a thread that "ticks" at a constant amount of times per second no matter how long the task it has to execute is. A task that takes longer than each tick would obviously not be possible and slow the number of ticks per second.
Thanks.
Use java.util.Timer.scheduleAtFixedRate:
public void scheduleAtFixedRate(TimerTask task,
Date firstTime,
long period)
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). As a consequence of the above, if the scheduled first time is in the past, then any "missed" executions will be scheduled for immediate "catch up" execution.
Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.
Here is a Thread version. A Timer would be the way to go but if you really want to use your own thread.
new Thread(new Runnable() {
#Override
public void run() {
try {
long before, sleepDuration, operationTime;
for(int i=0;i<100;i++) {
before = System.currentTimeMillis();
// do your operations
operationTime = (long)(1500*Math.random());
System.out.print("Doing operations for "+operationTime+"ms\t");
Thread.sleep(operationTime);
// sleep for up to 1000ms
sleepDuration = Math.min(1000, Math.max(1000 - (System.currentTimeMillis() - before), 0));
Thread.sleep(sleepDuration);
System.out.println("wait\t"+sleepDuration+"ms =\telapsed " + (operationTime+sleepDuration) + (operationTime > 1000 ? "<" : ""));
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
The Timer and TimerTask classes are perfect for your use.
Take a look at the docs http://docs.oracle.com/javase/6/docs/api/index.html?java/util/Timer.html or follow this lesson http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html
Let me know if I misunderstood your question.

Java: Run Script After Timed Interval

How do you activate or run a loop every tenth of a second? I want to run some script after an amount of time has passed.
Run a script every second or something like that. (notice the bolded font on every)
You can use a TimerTask. e.g.
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
You simply need to define a Runnable. You don't have to worry about defining/scheduling threads etc. See the Timer Javadoc for more info/options (and Quartz if you want much more complexity and flexibility)
Since Java 1.5:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new RepeatedTask(), 0, 100, TimeUnit.MILLISECONDS);
and
private class RepeatedTask implements Runnable {
#Override
public void run() {
// do something here
}
}
(remember to shotdown() your executor when finished)
You sleep.
Check out Thread.sleep() but be warned that you probably really don't want your whole program to sleep, so you might wish to create a thread to explicitly contain the loop and it's sleeping behavior.
Note that sleep only delays for a number of milliseconds; and, that number is not a perfect guarantee. If you need better time resolution you will have to use System.currentTimeMillis() and do the "time math" yourself. The most typical scenario is when you run something and you want it to run ever minute. The time the script runs must be captured by grabbing System.currentTimeMillis() before and after the script, and then you would need to sleep the remaining 1000 - scriptRunTime milliseconds.

Categories