java - Execute in future - time doubles somehow -


i sorry ask question going wrong code. playing around command patterns , want command class execute code in future - lets 2 seconds in future. problem somehow command gets executed in 5 seconds, not in 2??? each call decreases time variable until variable <= 0:

// 2 seconds in nanoseconds private long timeleft = 2000000000;  public boolean execute(final long delta) {     this.timeleft -= delta;     if (this.timeleft <= 0) {         // execute         this.timeleft = 2000000000l;     return true     }     return false; } 

with lastexecution = system.nanotime(); , each command delta = system.nanotime() - lastexecution;

full src on github: https://github.com/sheldor5/javagpp

instead of decrementing countdown timer value inaccurate deltas, calculate target time , check it, , use milli-time, not nano-time, prevent sign issues.

long targetmillis = system.currenttimemillis() + 2000; // 2 sec while (system.currenttimemillis() < targetmillis) {     // while wait } // 2 secs elapsed, may bit more 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -