1, Multithreading Introduction
The students who have used Thunderbolt know. Thunderbolt has a function called multithreading. Another is called offline download, we are here to focus on multi-threaded download. Multi-threaded, as the name implies is very many songs threads at the same time in the execution, 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 dug 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 the problem. Just want everyone to be clear about what is multi-threading and why there are multiple threads.
There has been a problem in multithreading, why do we have to propose multithreading? In fact, multithreading is to make full use of CPU hardware resources, to solve the problem of application waiting. Multithreading is to synchronize the completion of a number of tasks, not to improve the efficiency of implementation, but to improve the efficiency of resource utilization to improve the efficiency of the system. Threads are implemented at the same time as multiple tasks are required.
2. Ideas
(1) Get an Internet connection
(2) Initialization of multi-threaded download information, start to download
(3) Open up hard disk space for storing data resources
(4) Put the data obtained from the network into the application space
(5) Complete the download, close the resource link
Give a download of a 400M movie, as seen in the following:
Randomaccessfile support for Random interview
The Range Header field of the HTTP specifies where each thread starts downloading from the location of the file.
3. Code parsing
3.1 Set up information to download files
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 download links and start dividing the download section
URL url = new URL (this.downloadurl); if (This.data.size ()! = this.threads.length) {// Assume that the number of downloaded threads that were not previously downloaded 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);/Inference Line If the process has finished 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 finished downloading the task}}
3.4 Whether the listening file has been downloaded and the operation is completed
Fileservice.delete (This.downloadurl);//Suppose there are download records, delete them. Then join Fileservice.save (This.downloadurl, This.data) again;//write the downloaded real-time data to the database
Boolean notfinished = true;//download not completed//loop inference all threads are finished downloading while (notfinished) {notfinished = false;//Assuming all threads have been downloaded for (int i = 0; i < This.threads.length; i++) {if (this.threads[i]! = null &&!this.threads[i].isfinished ()) {//Assuming the discovery thread is not finished downloading notfinished = true;// Set flag for download not finished//assuming the download failed, and again on the downloaded data length based on the download if (this.threads[i].getdownloadedlength () = = 1) {//Once again open up the download thread. The code is consistent with the above}}}if (listener!=null) {listener.ondownloadsize (this.downloadedsize);} Notice that the length of the downloaded data is now}//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 to be in the download time, we for some reason, caused the download pause. For example, on the computer, our computer suddenly power off, the mobile phone network is interrupted, will cause the current download task to terminate, then when we come back again. The program should be able to continue to download, or the resources previously downloaded are wasted.
According to the description described above. We should be able to know. To achieve the continuation of the breakpoint, the key is to implement the downloaded data stored in the database, and then after our program again into the time, will go to the database to query data. Then continue with the download.
While storing data to a database is not too complicated, it is difficult to identify which data is being downloaded. What data is not downloaded. Here, we use the downloaded thread ID to do the identification when we download it.
Assume that the data for this thread ID is not fully downloaded. Should not be stored in the database, then this part of the data will be downloaded again, after the download, the data is stitched together is a complete file.
Android multi-threaded download large file parsing