I want to trigger a Java code at specific timing, let say every morning 1 AM.
I write a thread as below:
while (true) {
if (currentTime = 1AM) {
doSomething;
}
}
I am a bit worry, the while loop
keep running, will it slow down the machine, or eating up processing resource?
My first question is, I am thinking if I only loop 1 time per second, will it better?
while (true) {
if (currentTime = 1AM) {
doSomething;
}
Thread.sleep(1000);
}
My second question is, some time I see while loop
written as below, most of time to acquire the Java lock, can anyone explain how expensive if we write the while loop
like below (sorry if this is very basic question)?
while (isLock) {
// do nothing
}
doThisIfNoLock();
I extend the thought above, if I create a empty thread, and an infinite empty while loop
inside, how many resource (processing power) that actually eating up by the thread? Because there is no content inside the loop, based on my imagination, the loop will be running very fast, and end up taking many cycles of CPU power? Is that true?