Pausing a multi-threaded download

Hi,

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 =newbyte[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?

[1066 byte] By [chasana] at [2007-11-26 23:41:26]
# 1
Use a read timeout to limit the time each thread can block in the read.
ejpa at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...
# 2
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?
chasana at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...
# 3
Create it from SocketChannel.open().socket(), then you can close it asynchronously and the reading thread will get an AsynchrousCloseException. You will have to connect it yourself this way.
ejpa at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...
# 4
Could u plz post some sample code? How can I get the socket address of a particular URL? Also, how do I make use of Proxy?
chasana at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...
# 5
http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html#getHost() http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html#getPort()
ejpa at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...
# 6
But if I need to connect using a proxy server, how do I do that? Also, will I have to manually setup all the headers?
chasana at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...
# 7

OKOK forget the SocketChannel. You could just see what happens if you close the URLConnection asynchronously. It may cause an InterruptedIOException on some platforms, it may block on others, it may be ineffective on others. It has no defined behaviour unless it's an InterruptibleChannel, but it does work on some platforms.

ejpa at 2007-7-11 15:09:02 > top of Java-index,Java Essentials,New To Java...