Calculating time difference with start time and elapsed time - java

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.

Related

Updating graphics on an interval

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.

Explication java code for a beginner

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.

How to represent passing of time?

I'm creating a simple simulation of gas station as homework. The total duration of the simulation is the week. Filling cars is approximately 3 minutes depending on the type of fuel. Cars may be collected in a queue. Now the question. I know how to implement these methods, but have no idea how to simulate a period of time without methods like a Thread.sleep().
P.S. I'm using JavaFX framework for this task. Cars are represented as javafx.scene.shape.Rectangle and their movements through Tranlsate methods. Dispensers too.
The Thread.sleep() method accepts a millisecond value. Basically, you can run an update and then calculate how long you need to sleep until the next update.
You can measure real time elapsed with System.nanoTime(). Make sure your class implements Runnable. Inside the run method, stick a while loop which contains an update() method to update the cars. Get nano time at the start and end of the loop, subtracting the two which gives you elapsed time. Subtract the elapsed time from the time you want each update to take, then sleep the thread. I think that is really all you need.
Here is the code:
public void run() {
int updatesPerSecond = 5;
/* The target time is the time each update should take.
* You want the target time to be in milliseconds.
* so 5 updates a second is 1000/5 milliseconds. */
int targetTime = 1000 / updatesPerSecond;
long currentTime;
long lastTime = System.nanoTime();
long elapsedTime;
long sleepTime;
while (running) {
// get current time (in nanoseconds)
currentTime = System.nanoTime();
// get time elapsed since last update
elapsedTime = currentTime - lastTime;
lastTime = currentTime;
// run your update
update();
// compute the thread sleep time in milliseconds.
// elapsed time is converted to milliseconds.
sleepTime = targetTime - (elapsedTime / 1000000000);
// don't let sleepTime drop below 0
if (sleepTime < 0) {
sleepTime = 1;
}
// attempt to sleep
try {
Thread.sleep(sleepTime);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Within JavaFX framework,
you can simply use PauseTransition to simulate periodic time elapsing. On end of every period update scene graph elements' states. If you are going to change some properties of some node and do various animations you may utilize other types of Transitions. For more fine grained control you can use Timeline with its KeyFrames.

Android - CountDownTimer in ListView flickers randomly

I am making listview with timers, each with different deadline depending on the database(similar to auction)
Time now = new Time();
now.setToNow();
now.normalize(true);
nowMillis = now.toMillis(true);
.
.
String endtime = a.get(position).get(TAG_ENDTIME);
Integer timeSecond = Integer.parseInt(endtime.substring(17, 19));
Integer timeMinute = Integer.parseInt(endtime.substring(14, 16));
Integer timeHour = Integer.parseInt(endtime.substring(11, 13));
Integer timeDay = Integer.parseInt(endtime.substring(0, 2));
Integer timeMonth = Integer.parseInt(endtime.substring(3, 5)) - 1;
Integer timeYear = Integer.parseInt(endtime.substring(6, 10));
Time future = new Time();
future.set(timeSecond, timeMinute, timeHour, timeDay, timeMonth, timeYear);
future.normalize(true);
long futureMillis = future.toMillis(true);
long interval = futureMillis - nowMillis;
new CountDownTimer(interval,1000)
{
#Override
public void onTick(long millisUntilFinished)
{
Long interval = millisUntilFinished;
int days = (int) ((millisUntilFinished / 1000) / 86400);
int hours = (int) (((millisUntilFinished / 1000) - (days * 86400)) / 3600);
int minutes = (int) (((millisUntilFinished / 1000) - (days * 86400) - (hours * 3600)) / 60);
int seconds = (int) ((millisUntilFinished / 1000) % 60);
String countdown = String.format("%dd %dh %dm %ds", days, hours, minutes, seconds);
holder.duration.setText(countdown);
}
#Override
public void onFinish()
{
// TODO Auto-generated method stub
holder.duration.setText(TimeUp);
}
}.start();
That code works almost perfectly when there is only one instance.
However the problem arise when there is several instance, around 4-5 timer running at the same time
Several/all the countdown will start to flicker, be it seconds, minutes, hours, or days.
e.g. one of my timer flicker between 27d 11h 54m 50s and 0d 23h 47m 0s
Since this occur on both on emulator and on my device, it seems to be my code's flaw, but I don't have a clue what could cause this.
I tried to change
holder.duration.setText(countdown) into holder.duration.setText(millisUntilFinished)
and the the countdown flickers between the desired duration and a huge, random number,
Please help.
You should use one TimerTask and put all your UI updates into that single timer instead running multiple CountDownTimers for essentially the same job since you're already doing all the math to determine when "time is up" for any particular item, you might just run one TimerTask and once a second have it update everything. CountDownTimer is useful for a single implementation count down because it does some built-in math, etc. You're redoing all that math, so you might as well use one instance of a regular TimerTask.
The implementation of CountDownTimer relays on scheduled delays in Handler messaging. A single countdown instance is unlikely to result in any bizarre behavior, but if you have several going that all supposed to "tick" when the system clock hits each second (the time in millis ends in "000" - once a second, and all at the same time), then those handlers will all try to fire simultaneously and inevitably fail.
If the UI or other process will likely delay some of these messages, even to the point where it will "skip ticks" to catch up. Also, that means that the next message delay could be only milliseconds from the next tick (i.e. if it's supposed to check every 1000 millis, but is delayed an additional 1990 millis, then it will skip a tick and also schedule the next message for 10 millis into the future.

schedule management

Finally after some trial and errors I have managed to make it work as I wanted.
But now I would like your advice to make the code more readable and simple it seems a made a lot of unnecessary code to archive what I wanted.
What this basicly do is, if you turn on the server app at a time a schedule task should be running, it will start the task and let it run for the time left from when it should have started otherwise it will be schedule to run at the hour it is supposed to run.
So if the schedule time is 13:00:00 and should run for 120 minutes and you start the app at 13:30 it will run for 90 minutes. If you start it after that time, it will be normally schedule for the next day 13:00:00.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
if (start_time < 0)
{
long minutes = (start_time*-1) / (60 * 1000);
if (minutes > 0 && minutes < 120)
{
runTimeLeft = 120 - minutes;
ThreadPoolManager.getInstance().schedule(new Runnable()
{
public void run()
{
myTask();
}
}, 0);
}
else
runTimeLeft = 0;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour+24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
}
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
public void run()
{
myTask();
}
}, start_time, 24 * 60 * 60 * 1000);
So my question here now is what could I improve on the above code ?
Instead of using java.util.Timer alone, try using it with TimerTask. There is a good article from IBM on this.
Have a look at this link: http://www.ibm.com/developerworks/java/library/j-schedule.html
The code is also shared and seems to work for trivial routine job.
Use this instead for your first method:
int interval = 24 * 60 * 60 * 1000; // might be long instead of int
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
public void run()
{
myTask();
}
}, interval, interval);
This will create a simple timer that will call myTask() in 24 hours, and then every 24 hours after.
Your other requirement is a little different, though. If I understand your description correctly, you basically want your app to always execute some task at 12:00 AM if it happens to be up and running. If you don't care about down-to-the-millisecond accuracy for this, you could achieve it very simply by starting a Timer with a one minute period and checking the current system time in each tick - when you hit 12:00 AM run your daily task.
A fancier way would involve interacting with the OS such that it makes callbacks to your application at pre-scheduled times (possibly even starting your app if necessary), but this kind of thing is OS/platform specific (which you didn't specify).
Update: I know nothing about Linux, but it looks like a cron job is what you're looking for. See this question:
Running a scheduled task written in java on a linux server

Categories