Pausing a multi-threaded download

C

Chandru

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?
 
D

Daniel Pitts

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?

I believe you can call Thread.interrupt(), and catch the
InterruptedIOException.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top