• Post Reply Bookmark Topic Watch Topic
  • New Topic

Downloading a file with time out  RSS feed

 
Andrew Aldoc
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I currently have the following code that I use to download a file from a website:



Usually, the code works very well. But sometimes, the download gets stalled indefinitely. Is there a way to set a time out period for the downloading of this file?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Aldoc wrote:Usually, the code works very well. But sometimes, the download gets stalled indefinitely. Is there a way to set a time out period for the downloading of this file?

As with most questions of this kind, the answer is almost always: Yes. But exactly how you do it will depend very much on what you want to occur.

Easiest is probably as follows:
1. Put that code in a Thread or a Runnable that you can check to see whether it has "finished" or not (this can be simple boolean flag that you only set to true when you know that the process is complete; just be sure that you use synchronized methods to read and change it).
2. Kick it off.
3. Wait 10 seconds (or however long you fancy).
4. Check to see if it's finished, and if not do something.

The only problem may be - and TBH, I'm not enough of an expert in these things to know - that it might be quite difficult to actually stop the downloading Thread if it really is "blocked". Most of the methods designed to do this (like stop() and destroy()) are deprecated, and so shouldn't be used.

You can use interrupt(), but I suspect there might be situations in which it could just be ignored or never cause the Thread to react, because it's "stuck".

I'm sure, however, that there are good patterns for this stuff; I'm afraid I just don't know what they are. However, setting up a separate thread to do this is where I'd start.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!