File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting time out in filesearch program

 
Abhishek Rath
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

     
    Abhishek Rath
    Ranch Hand
    Posts: 54
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the details.can you please post the sample code to set timeout in a thread if you have any.
     
    Nitesh Kant
    Bartender
    Posts: 1638
    IntelliJ IDE Java MySQL Database
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ShowSomeEffort.
    Do you have something that you have worked on?
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic