Run Java program forever (loop)

JaZziD
JaZziD used Ask the Experts™
on
Anyone has a simple example of java program that can run forever (say like within a few second).

My program is going to run forever scan a directory within every a few second then grabs the files in it.

How do i run it forever? Bit confused.

Need your help guys!

Thanks,
A
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
you can create a winnt service and run as an service

Author

Commented:
I want to know the pros and contras of using Timer class to run my java program in a loop (forever) within every few seconds to scan a directory.

This program will sit in a server and run continously NON STOP.

Is this the correct way of how to do this? Or is there any other way i can use?


public class ScanDir {
   Timer timer;

   public Reminder(int seconds) {
       timer = new Timer();
       timer.schedule(new ScanDir(), 0, seconds*1000);
   }

   class ScanDir extends TimerTask {
       public void run() {
           //SCAN THE DIRECTORY AND GRAB THE XML FILE
       }
   }

   public static void main(String args[]) {
       new ScanDir(5);
   }
}


THANKS VERY MUCH!!!!
There is a very easy way:

public class Scanner
{

public void scanDirectory()
{
    // do scanning
}

public void go()
{
    while(true)
    {
        scanDirectory();
        Thread.sleep(10000); // will sleep for 10 seconds
    }
}

}

Bingo
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

willstones has give you a simple anwser. You can write a simple loop-forever loop like
while(true)
{
   doYourJob()
   Thread.sleep(enoughTime);
}

or for(;;)
{
   doYourJob()
   Thread.sleep(enoughTime);
}


The rest is what kind of programm you want to write. If this is a standalong program you can use it as the main loop of you programm. Otherwise if it is a part of a program then you should pack-it in a procedure or function and run it in a seperate thread. If you want also to remote control it you will need also some kind of communcation between thread's (e.g. you want to "start" and "stop" it from you application menu, so the user-menu thread should change a shared variable that it is tested in loop
e.g.

while(SharedVariableForRun)
{
   doYourJob()
   Thread.sleep(enoughTimeToRepeat);
}

Sleep is necessary because if you forget it your thread will always call the doYourJob() and consume all the avaliable cpu cycles and this will slow down then performance of other threads or processes.

for further informations take a look on thread programming.

Commented:
both willstones and AccessDenied solutions are right

Thanks

Quick response??? ;)

Author

Commented:
Thanks guys...
Wish i could accept 2 answers as the correct one...
Sorry AccessDenied :)

However, thanks so much for the help guys!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial