Background App

A

Andrew Tucker

I have written a small program to monitor a given file and when that file is
modifed, to back it up (Like a mirror idea). The program consists of an if
statement w/in a while(true), ie it loops forever, this is chewing up my cpu
with constant 'activity', is there an easier way to do this, would i be
better to just compare every couple of minutes or is there a way of making
things run in the background and only react when the file is changed. I
haven't explained this very well but i hope someone can point me in the
right direction!

cheers, Andrew.
 
A

Andy Fish

AFAIK there is no way to receive a notification when a file is changed. For
windows you could do this using JNI but it would be platform specific.

If you need to go the polling route use Thread.sleep() to pause the program

Andy
 
B

Brad BARCLAY

Andrew said:
I have written a small program to monitor a given file and when that file is
modifed, to back it up (Like a mirror idea). The program consists of an if
statement w/in a while(true), ie it loops forever, this is chewing up my cpu
with constant 'activity', is there an easier way to do this, would i be
better to just compare every couple of minutes or is there a way of making
things run in the background and only react when the file is changed. I
haven't explained this very well but i hope someone can point me in the
right direction!

Most OS's don't have any sort of subsystem that will allow the
filesystem to report whenever a specific file has been changed, so there
isn'treally any better way to do this in Java other than to poll.

However, you shouldn't poll in a busy-loop without adding some wait
time. It doesn't have to be very long -- even a 5 second wait will
cause a huge performance increase. Find the point where you want to
pause for a short period of time, and add the code:

try {
synchronized(this) {
wait(5000l);
}
} catch (InterruptedException e) {
// Who cares? :)
}

This should fix your problem. HTH!

Brad BARCLAY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top