简体   繁体   中英

Time based calculation in Java

I am creating a program that makes a calculation every minute. I don't know exactly how to do this efficiently, but this is some pseudo code I have written so far:

 stockCalcTimerH = System.currentTimeMillis() - 1;
    stockCalcTimerI = stockCalcTimerH;
    stockCalcTimer = System.currentTimeMillis();

    if (stockCalcTimerI < stockCalcTimer) {
      *do calcuations*
     stockCalcTimerI + 60000;

When I print both values out on the screen, it comes out as this:

stockCalcTimerI = 1395951070595 stockCalcTimer = 1395951010596

It only subtracts the number, and doesn't add the 60000 milliseconds... I'm kind of new to Java, but any feedback helps.

Thanks for reading!!

stockCalcTimerI + 60000;

The new value never gets assigned to a variable.

Change that to:

stockCalcTimerI += 60000;

Which is the same as

stockCalcTimerI = stockCalcTimerI + 60000;

You can use java.util.Timer class to schedule runs.

TimerTask task = new TimerTask() {
    @Override public void run() {
        // do calculations
    }
};
int delay = 0;          // no delay, execute immediately
int interval = 60*1000; // every minute
new Timer().scheduleAtFixedRate(task, delay, interval);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM