Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will an infinite 'while loop' consume excessive resources

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?

like image 312
Sam YC Avatar asked Jul 29 '16 06:07

Sam YC


People also ask

What will happen if an infinite while loop runs?

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.

What is the danger of an infinite loop?

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.

Is it good to use infinite loop?

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.

What happens if you write an infinite loop and the computer runs out of memory?

Even if your loop is infinite, it will keep running and once it runs out of memory, it will crash and automatically get terminated.


2 Answers

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.

like image 187
Yogesh_D Avatar answered Sep 24 '22 16:09

Yogesh_D


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.
like image 27
Ryan Avatar answered Sep 26 '22 16:09

Ryan