2

I am stater in java programming ... I am writing a scheduler. I am reading date and time values from a properties file. If there are any changes in the properties file then i have to reschedule the scheduler.. so for this i am writing an infinite for loop as shown:

for(;;){
    result = loadData.checkChanges();
    if(result == true){
        //If changes found in the properties file then reSchedule tasks
        reSchedule();
    }
}

If the loop runs until the life of the application in "application server"(one year or two years), will it encounter any performance problems or JVM problems or are there any other problems?

Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54
krishna
  • 21
  • 1

3 Answers3

3

Unless your checkChanges() method sleeps or blocks, the code as currently written will thrash the CPU by calling loadData.checkChanges(); as fast as the processor is able. Ideally you should do something like call loadData.wait(), and then have another thread call loadData.notify() when you would like the check to run. A simpler and nearly as good solution is to sleep the thread periodically, like:

for(;;){
    result = loadData.checkChanges();
    if(result == true){
        //If changes found in the properties file then reSchedule tasks
        reSchedule();
    }
    try {
        Thread.sleep(100);
    }
    catch (InterruptedException ignored) {}
}

Also, please don't use for(;;). Instead try while(true), it's so much clearer. Or even better, try while(!stop) where stop is a boolean variable that gets set to true when your application context is destroyed (server shutdown, webapp undeploy, etc.).

aroth
  • 54,026
  • 20
  • 135
  • 176
  • 2
    `for(;;)` is a style choice. It's my personal preference for infinite loops (and any other loops with a non-standard exit point) and is read "for ever". Some people go so far as to do a `#define ever ;;` in languages with a preprocesser and then use `for (ever)`. – Mark Peters Jun 20 '11 at 02:00
  • @Mark - It's a style choice that is confusing to many and that has clearer alternatives that are nearly as concise. The question of "What does `for(;;)` do?" shows up on SO fairly regularly, but I don't think I've ever seen an equivalent "What does `while(true)` do?" post. – aroth Jun 20 '11 at 02:05
  • 1
    @aroth : People who need to ask the question are not cut out to be a programmers. Although there are arguably ways to do it that are stylistically better, it's a widely adopted practice that has been in used for over 40 years. The many questions asked and answered on it show the inability of the asker to use a search engine, read a book or otherwise exercise independent though, all requirements to be a good developer. – mattnz Jun 20 '11 at 02:20
  • 1
    @mattnz: People who write obfuscatory code that doesn't immediately and clearly state up-front what it does are also not cut out to be programmers in the real world (that being the world where other people have to read the stuff your fingers defecate out). – JUST MY correct OPINION Jun 20 '11 at 03:12
  • @mattnz - The measure of a good programmer isn't their ability to decipher cryptic language constructs. As languages and development tools become increasingly abstract and higher level this will only become more true. Programming nowadays is much more about design, modeling, and abstraction than it was 40 years ago. A good programmer understands conceptually what an infinite loop is, and why they can be useful. Whether or not they immediately recognize `for(;;)` as an infinite loop is really neither here nor there. – aroth Jun 20 '11 at 03:47
  • 1
    @aroth @JUST. All I am saying that for(;;) is so widely used it has past the point of being the "obfuscatory code" we all despise so much, and there more important problems than whose got the better dress sense. – mattnz Jun 20 '11 at 04:48
1

This is going to be huge cpu hogger as this is running an infinite loop without any polling or wait mechanism. Better to use java Executor API for this kind of job: http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executor.html

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    We don't know that there isn't any wait mechanism as `checkChanges()` isn't given. How does the Executor API apply here? – Mark Peters Jun 20 '11 at 01:59
1

As already stated, the CPU will max out. But you can easily prevent that by waiting for some fixed amount of time, e.g. 1 sec, at the end of the loop:

try { Thread.sleep(1000); } catch (InterruptedException ex) {}
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95