wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Setting time out in filesearch program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Setting time out in filesearch program" Watch "Setting time out in filesearch program" New topic
Author

Setting time out in filesearch program

Abhishek Rath
Ranch Hand

Joined: Nov 22, 2007
Posts: 38
Hi

I have a class which list down all the files in a folder recursively.I need to set a timeout functionalty so that if the code does not return the data with in the specified time limit then it will time out the search and displayes an error message.

Please let me know how to implement the same.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

There are two places where your program can take time:

  • Query the filesystem
  • Process all the files {really deeply nested directories}

  • If it is 1 above then whether you can terminate depends on whether querying the filesystem is interruptible.
    In your case you would essentially be listing the files in a directory which is not interruptible, so the only place you can really timeout your program is when you are about to query a new directory. This can be done in 2 ways:

  • Have a different thread checking after every X minutes and if the timeout expires, flag the program to stop (this flag your program will check before every recursion).
  • Embed the timeout logic inside your program.

  • I will go for 2 since there is no real need for spawning a new thread when you can not do much when processing is going on.
    If your IO action was interruptible, it would make sense to go for 1.


    apigee, a better way to API!
    Abhishek Rath
    Ranch Hand

    Joined: Nov 22, 2007
    Posts: 38
    Thanks for the details.can you please post the sample code to set timeout in a thread if you have any.
    Nitesh Kant
    Bartender

    Joined: Feb 25, 2007
    Posts: 1638

    ShowSomeEffort.
    Do you have something that you have worked on?
     
     
    subject: Setting time out in filesearch program
     
    Similar Threads
    Yes,can we now ask, where are the WMD?
    again session
    script time out & page time out in JSP
    What's up with Eclipse.org
    Kill the Session