Android multi-threaded download large file parsing

Source: Internet
Author: User

1, Multithreading Introduction

Have used the thunder of the students know that Thunderbolt has a function called multi-threading, there is a call offline download, we focus on the introduction of multi-threaded download. Multithreading, as the name implies is a lot of song threads running at the same time, why to put forward the concept of multithreading? Because sometimes a thread downloads too slowly. For example, as a child often do math problems, a person digging ditch need 15 days, then two people to dig it?

Of course the math problem above two people's efficiency is not the same, we here to simplify this problem, just want to understand, what is multi-threading, why have multi-threading.

There has been a problem in multithreading, why do we have to propose multithreading? In fact, the multithreading is to make full use of the CPU hardware resources, to solve the application waiting problems. Multithreading is to accomplish many tasks synchronously, not to improve the efficiency of the operation, but to improve the efficiency of the use of resources to improve the efficiency of the system. Threads are implemented at the same time when multiple tasks need to be completed.


2. Ideas
(1) Get an Internet connection
(2) Initialization of multi-threaded download information, start the download
(3) Open up hard disk space for storing data resources
(4) Put the data obtained from the network into the application space
(5) Download complete, close resource link

Give a download of the 400M movie, as shown below:


Randomaccessfile support for random access
The Range header field of HTTP specifies where each thread starts the download from where the file is located.


3. Code parsing

3.1 Set up information to download the file

Randomaccessfile randout = new Randomaccessfile (This.savefile, "RWD"), if (this.filesize>0) {randout.setlength ( this.filesize)///Set File size}randout.close ();//Close the file for the settings to take effect

3.2 Set up the download link and start dividing the download section

URL url = new URL (this.downloadurl); if (This.data.size ()! = this.threads.length) {// If you have not downloaded or the original number of download threads is inconsistent with the current number of threads this.data.clear ();//Empty the data for (int i = 0; i < this.threads.length; i++) {// Traverse thread pool this.data.put (i+1, 0);//Initialize each thread has downloaded a data length of 0}this.downloadedsize = 0;//Set the length of the download is 0}


3.3 Start Download file

for (int i = 0; i < this.threads.length; i++) {//Gets the length of data that the thread has downloaded by a specific thread id, int downloadedlength = This.data.get (i+1);//Judgment Line Process has completed downloading, otherwise continue downloading if (Downloadedlength < This.block && This.downloadedsize < this.filesize) {// Initialize the thread of a specific ID this.threads[i] = new Downloadthread (this, URL, This.savefile, This.block, This.data.get (i+1), i+1);// Set the priority of the thread, thread.norm_priority = 5 Thread.min_priority = 1 thread.max_priority = 10this.threads[i].setpriority (7);// Start thread This.threads[i].start ();} Else{this.threads[i] = null;//indicates that the thread has completed the download task}}


3.4 Whether the listening file is downloaded and completed

Fileservice.delete (This.downloadurl);//If there are download records, delete them, and then add Fileservice.save (This.downloadurl, this.data);// Write live data that has already been downloaded to the database


Boolean notfinished = true;//Download Incomplete//loop to determine if all threads are finished downloading while (notfinished) {notfinished = false;//assumes all threads download complete for (int i = 0; i < This.threads.length; i++) {if (this.threads[i]! = null &&!this.threads[i].isfinished ()) {//If discovery thread does not complete download notfinished = true;// Set flag for download not completed//If the download fails, then re-download the data length based on the downloaded if (this.threads[i].getdownloadedlength () = = 1) {//re-open the download thread, the code is consistent with the above}}}if ( Listener!=null) {listener.ondownloadsize (this.downloadedsize);} Notifies the currently downloaded data length}//download complete Delete record if (downloadedsize = = this.filesize) {fileservice.delete (this.downloadurl);}

4, the breakpoint continues to pass

The continuation of the breakpoint is said at the time of downloading, we for some reason, caused the download pause, such as on the computer, our computer suddenly power off, the phone network interruption, will cause the current download task to terminate, then when we come back again, the program should be able to continue to download, Otherwise, the resources that were downloaded before are wasted.
According to the above description, we should be able to know that the implementation of the continuation of the breakpoint, the key is to implement the download of the data stored in the database, and then after our program re-entry, will go to the database to query the data, and then continue to download. While storing the data to the database is not too complex, it is difficult to identify which data of the program is downloaded, which data is not downloaded, here we use the download of the thread ID to do the identification.
If the data of the thread ID is not fully downloaded, it should not be stored in the database, then this part of the data will be re-downloaded, after the download is complete, the data is a complete file to be stitched up.




Android multi-threaded download large file parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.