This month, I received a project to write an APP for software management like a 360 assistant. I encountered a problem: multi-thread breakpoint download.
This is because you have not previously written this application function. Therefore, you have to learn it by yourself. Then we collected and sorted it out on various websites. I would like to share with you some further to deepen my understanding in this regard.
What is multi-thread download?
Multi-thread download is actually thunder. Some download principles of BT are connected to the server through multiple threads at the same time, so you can squeeze a higher bandwidth, the general practice is to cut the file into N blocks, each of which is handed to a separate thread for download. After each download is complete, the file blocks are combined into one file, small algorithms for cutting and assembly in the program
What is resumable data transfer?
Resumable download: When the download process is not completed, the download progress is exited. When the next download attempt is enabled, the download continues with the previous download progress, the principles and implementation methods of resumable data transfer are as follows.
How to complete multi‑thread resumable data transfer?
To combine the two into one, the program needs to remember the download progress of each file block and save it to the database. When the download program starts, you need to determine whether the program has downloaded the file, take out the storage records of each file block, and calculate the download progress to continue the download. here you need to master the basic knowledge of java multithreading, handler usage, collection, and algorithm, file Operations and other basic skills, but also to solve the issue of sqlite Database Synchronization, because it does not support multi-threaded operations, poor control, often encounter database locking exceptions, at the same time, you cannot directly operate on http in the activity after android2.3. Otherwise, you will receive the NetworkOnMainThreadException sent by the system. In terms of the UI experience, remember to use Asynchronization.
After learning about this, let's take a look at the source code ......
Let's take a look at the following ,:
Zookeeper --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
As for some of the code, I will not post it here; I think it is convenient to directly view the source code.
Click here to download the source code.