I am a beginner java programmer and i saw a code on internet about my projet.
But i didn't understand what it does ? Anyone can explain ?
What are 1000s ?
private Timer timer = null;
private int timeWorking;
private void xxxxxxxxxxx() {
if (timer == null) {
timer = new Timer("Time");
timer.schedule(new TimerTask() {
#Override
public void run() {
timeWorking++;
}
}, 1000, 1000);
}
}
This is a call to java.util.Timer.schedule(TimerTask task, long delay, long period):
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
Both delay and period are in milliseconds. 1000 milliseconds are equal to one second.
The see documentation of Timer.schedule()
task - task to be scheduled.delay -
delay in milliseconds before task is to be executed.period -
time in milliseconds between successive task executions.
See java.util.Timer documentation
The first "1000" means delay - delay in milliseconds before task is to be executed.
The second "1000" means period - time in milliseconds between successive task executions.
public void schedule(TimerTask task, long delay, long period) you are calling this method with delay 1000ms and period 1000ms.
Related
I have the code below that prints a text every day. However I would like it to print the text only on -- for example -- 8 o'clock in the morning.
String text = "Test";
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(text);
}
}, 0, 1000 * 60 * 60);
In my code, the print time depends on the time I run the code and the code of course does not consider the daylight saving time changes etc.
Use public void schedule(TimerTask task, Date time) of Timer
I am looking for the best way in Java to monitor the computer clock (the minutes) and to fire off a method/thread every time it changes.
So if the time is 13:20 and it changes to 13.21 then do something. So any time there is a minute change some code gets fired.
What is the best way to listen to the minute section of the clock for changes ?
Thanks,
Richard
Find the current system time using System.currentTimeMillis()
Calculate how many milliseconds until the next minute
Schedule a TimerTask on a Timer to run in that number of milliseconds in the future
In that TimerTask's event handler schedule a new reoccurring TimerTask to run every 60,000 milliseconds.
int milisInAMinute = 60000;
long time = System.currentTimeMillis();
Runnable update = new Runnable() {
public void run() {
// Do whatever you want to do when the minute changes
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
update.run();
}
}, time % milisInAMinute, milisInAMinute);
// This will update for the current minute, it will be updated again in at most one minute.
update.run();
Sounds like a job for Quartz. You can do this using the following cron expression:
0 * * * * ?
Date d = new Date(System.currentTimeMillis());
Then you can get the minutes by d.getMinutes(). Have a check running in a thread waiting for the value of d.getMinutes() to change.
What's the best way to update a screen every interval of time t for graphics? Assuming the update method is called with update(), and all actual graphics stuff takes place there and is already handled and everything.
I've used the javax.swing.Timer which triggers an action event ever specified interval of time to run my update methods in the past, however the speed of the computer is still a factor. So if I'm doing it the best way (which I doubt) how do I leave updating exclusive to time?
The system clock calls in Java aren't necessarily super accurate. Here's one way to take into account update time when waiting for a specified period.
In this example, we're updating every 40 milliseconds, or 25 frames a second.
package com.ggl.testing;
public class TimerRunnable implements Runnable {
private static final long interval = 40L; // 40 milliseconds
#Override
public void run() {
long startTime = System.currentTimeMillis();
while (true) {
update();
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
elapsedTime = Math.max((interval - elapsedTime), 5L);
sleep(elapsedTime);
startTime = System.currentTimeMillis();
}
}
private void sleep(long interval) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
}
}
private void update() {
}
}
We see how long it takes to perform the update method. We calculate the elapsed time, and make sure the interval minus the elapsed time doesn't fall below 5 milliseconds. if it takes longer than 35 milliseconds to do the update, we will drop the frame rate to keep up.
public static final long TIMEOUT = 60000;
public static final long SYSTEM_TIME = System.currentTimeMillis();
I have the TIMEOUT Value for my application set as 60000 and i have my system time. Now how would i know that 50 seconds has been elapsed and i need to show a message to the end-user.
if (TIMEOUT - SYSTEM_TIME <= 10000) {
Toast.makeText(getApplicationContext(), "10 Seconds Left", Toast.LENGTH_LONG).show();
disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}
If you don't need to do other stuff in that thread you can use a sleep(50000).
This is how to run a specific task one-shot:
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// TODO
...
}
}, TIMEOUT);
The doc is here (as reported by jimpanzer)
Maybe that you can use something like this :
long startTime = SystemClock.elapsedRealtime();
// do what you want
long endTime = SystemClock.elapsedRealtime();
long ellapsedTime = endTime - startTime;
if (ellapsedTime>TIME_OUT) {
// do stuff
}
Maybe I am wrong or just not had enough coffee yet, but Timeout is 60000, your System value is much more - all millis starting at the year 1970 (if I am not mistaken here as well). This means your result from TIMEOUT - SYSTEM_TIME is negative and therefor a negative number and therefor smaller than 10000. So your if-statement always runs.
I am looking for the best way in Java to monitor the computer clock (the minutes) and to fire off a method/thread every time it changes.
So if the time is 13:20 and it changes to 13.21 then do something. So any time there is a minute change some code gets fired.
What is the best way to listen to the minute section of the clock for changes ?
Thanks,
Richard
Find the current system time using System.currentTimeMillis()
Calculate how many milliseconds until the next minute
Schedule a TimerTask on a Timer to run in that number of milliseconds in the future
In that TimerTask's event handler schedule a new reoccurring TimerTask to run every 60,000 milliseconds.
int milisInAMinute = 60000;
long time = System.currentTimeMillis();
Runnable update = new Runnable() {
public void run() {
// Do whatever you want to do when the minute changes
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
update.run();
}
}, time % milisInAMinute, milisInAMinute);
// This will update for the current minute, it will be updated again in at most one minute.
update.run();
Sounds like a job for Quartz. You can do this using the following cron expression:
0 * * * * ?
Date d = new Date(System.currentTimeMillis());
Then you can get the minutes by d.getMinutes(). Have a check running in a thread waiting for the value of d.getMinutes() to change.