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?
Infinite Loops A common infinite loop occurs when the condition of the while statement is set to true . Below is an example of code that will run forever. It is not necessary to test any infinite loops. An infinite loop will run forever, but the program can be terminated with the break keyword.
The harm that Infinite Loop can do Same as any other process Once started infinite loop will use your processor time and power. And at one point in time, it slows down your system and made it unresponsive. By using your memory — In computer mainly all running processes reside inside the RAM.
Infinite loops are useful if you want your script (or part of the code) to run endlessly, until a specific action is taken. Especially, when there can be more than one action that stops your script or block of code from working. In event-based programming paradigm endless loops are very common.
Even if your loop is infinite, it will keep running and once it runs out of memory, it will crash and automatically get terminated.
Its not a good practice to run a infinite while loop as it can hog the CPU.
For Scheduled tasks you can use either libraries like Quartz or use java.util.Timer
and java.util.TimerTask
Your Task needs to inherit from TimerTask
and override the run()
method.
And then you need to use Time.schedule()
method to schedule your task.
Thread.sleep
can be used in this scenario however a better option would be using a timer similar to this
long current = System.currentTimeMillis();
while(current < expectedElapsedTime){
current = System.currentTimeMillis();
//logic goes here
}
Running a infinite loop without some sort of timer to slow the loop down will suck all your resources out of your computer like a vacuum. Another way you can schedule tasks is with a swing timer
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent event){
checkUser = true;
}
};
Timer checkUserTimer = new Timer(5000, listener); // 5 second pause
checkUserTimer.start(); // start the timer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With