I need to create an object that will stop execution for a certain amount of time using its own class methods. How do I have a program keep track of passing time and execute a function when a specified amount of time has passed.
I imagine …….
long pause; //a variable storing pause length in milliseconds.............
long currentTime; // which store the time of execution of the pause ,.............
and when another variable tracking time has the same value as currentTime + pause, the next line of code is executed. Is it possible to make a variable that for a short period changes every millisecond as time passes?
For a simple solution, you could just use
Thread#sleepWith a timer…
And with a loop
Note, this is more expensive then the other two solutions as the loop continues to consume CPU cycles, where as the
Thread#sleepandTimeruse a internal scheduling mechanism that allows the threads to idle (and not consume cycles)