JaZziD
asked on
Run Java program forever (loop)
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
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
you can create a winnt service and run as an service
ASKER
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!!!!
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!!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(enoughTimeToR epeat);
}
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.
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(enoughTimeToR
}
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.
both willstones and AccessDenied solutions are right
Thanks
Thanks
Quick response??? ;)
ASKER
Thanks guys...
Wish i could accept 2 answers as the correct one...
Sorry AccessDenied :)
However, thanks so much for the help guys!!!
Wish i could accept 2 answers as the correct one...
Sorry AccessDenied :)
However, thanks so much for the help guys!!!