Having recently worked on a project which required some more IO interaction than I'm used to, I felt like I wanted to look past the regular libraries (Commons IO, in particular) and tackle some more in depth IO issues.
As an academic test, I decided to implement a basic, multi-threaded HTTP downloader. The idea is simple: provide a URL to download, and the code will download the file. To increase download speeds, the file is chunked and each chunk is downloaded concurrently (using the HTTP Range: bytes=x-x
header) to use as much bandwidth as possible.
I have a working prototype, but as you may have guessed, it's not exactly ideal. At the moment I manually start 3 "downloader" threads which each download 1/3 of the file. These threads use a common, synchronized "file writer" instance to actually write the files to disk. When all threads are done, the "file writer" is completed and any open streams are closed. Some snippets of code to give you an idea:
The thread start-up:
ExecutorService downloadExecutor = Executors.newFixedThreadPool(3);
...
downloadExecutor.execute(new Downloader(fileWriter, download, start1, end1));
downloadExecutor.execute(new Downloader(fileWriter, download, start2, end2));
downloadExecutor.execute(new Downloader(fileWriter, download, start3, end3));
Each "downloader" thread downloads a chunk (buffered) and uses the "file writer" to write to disk:
int bytesRead = 0;
byte[] buffer = new byte[1024*1024];
InputStream inStream = entity.getContent();
long seekOffset = chunkStart;
while ((bytesRead = inStream.read(buffer)) != -1)
{
fileWriter.write(buffer, bytesRead, seekOffset);
seekOffset += bytesRead;
}
The "file writer" writes to disk using a RandomAccessFile
to seek()
and write()
the chunks to disk:
public synchronized void write(byte[] bytes, int len, long start) throws IOException
{
output.seek(start);
output.write(bytes, 0, len);
}
All things considered, this approach seems to work. However, it doesn't work very well. I'd appreciate some advice/help/opinions on the following points. Much appreciated.
InputStream
almost never actually fills the buffer, which causes more IO writes than I would like. I'm under the impression that in this scenario, it would be best to keep the IO access to a minimum, but I don't know for sure whether this is the best approach.Note: I use Apache HTTPClient to do the HTTP interaction, which is where the entity.getContent()
comes from (in case anyone is wondering).
The most efficient approach for download is 2 threads: one for the UI, and the other for the download so that any pauses/dealys don't stall the user interface.
Java has great support for multithreaded applications. Java supports multithreading through Thread class. Java Thread allows us to create a lightweight process that executes some tasks. We can create multiple threads in our program and start them.
Principle of multi thread Download Client to download a file, first request the server, the server will transfer the file to the client, the client saved to the local, completed a download process. The idea of multi thread download is that the client starts multiple threads to download at the same time.
Multithreading and Multiprocessing are used for multitasking in Java, but we prefer multithreading over multiprocessing. This is because the threads use a shared memory area which helps to save memory, and also, the content-switching between the threads is a bit faster than the process.
To answer my own questions:
while() {}
loop that was waiting for the threads to finish. As it turns out, awaitTermination
is a much better alternative to wait for an Executor
to finish :)Presumably the Apache HTTP client will be doing some buffering, with a smaller buffer. It will need a buffer to read the HTTP header reasonably, and probably handling chunked encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With