Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Pausing a multi-threaded download

Thread view: 
Chandru - 09 Apr 2007 11:59 GMT
I'm working on a multi-threaded download manager. The code given below
is run by several threads. The code tries to read a set of bytes from
a URL and writes it to a random access file. Each threads writes to a
different part of the file, hence the need for seek().

byte [] data = new byte[1024];
int readLen;
while(canContinue && (readLen = bin.read(data)) != -1) {
    synchronized(dest) {
        dest.seek(start+completed);
        dest.write(data, 0, readLen);
        completed += readLen;
    }
}

Now when the user pauses the download, canContinue is set to false.
The problem with this scheme is that, it takes some time to pause all
the download threads. How can I stop the downloads instantaneously?

I've set readTimeout to 10secs. If I set it to a lower value, there
would be too many retries as my application tries to reestablish
connection when timeout occurs and the download is not paused. I there
any way to destroy a connection abruptly?
Daniel Pitts - 09 Apr 2007 16:48 GMT
> I'm working on a multi-threaded download manager. The code given below
> is run by several threads. The code tries to read a set of bytes from
[quoted text clipped - 20 lines]
> connection when timeout occurs and the download is not paused. I there
> any way to destroy a connection abruptly?

I believe you can call Thread.interrupt(), and catch the
InterruptedIOException.
Chandru - 16 Apr 2007 10:58 GMT
> I believe you can call Thread.interrupt(), and catch the
> InterruptedIOException.

Thanx man. It worked!!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.