Background App

Discussion in 'Java' started by Andrew Tucker, Jul 31, 2003.

  1. 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.
    Andrew Tucker, Jul 31, 2003
    #1
    1. Advertising

  2. Andrew Tucker

    Andy Fish Guest

    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

    "Andrew Tucker" <> wrote in message
    news:3f28cd11$...
    > 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.
    >
    >
    Andy Fish, Jul 31, 2003
    #2
    1. Advertising

  3. Andrew Tucker

    Brad BARCLAY Guest

    Andrew Tucker wrote:
    > 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

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    
    Brad BARCLAY, Jul 31, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mason66
    Replies:
    0
    Views:
    421
    mason66
    Jul 27, 2006
  2. JWL
    Replies:
    4
    Views:
    584
    Michael Winter
    Sep 26, 2006
  3. jc
    Replies:
    3
    Views:
    1,727
  4. jc
    Replies:
    1
    Views:
    1,362
    Neredbojias
    Mar 19, 2008
  5. Dj Frenzy
    Replies:
    3
    Views:
    307
    Robert
    Feb 10, 2004
Loading...

Share This Page