Explain the multi-threaded breakpoint download in Android

First, let's look at the principle of multithreaded download. Multi thread download is to divide the original files on the same network into equal parts according to the number of threads, then each individual thread downloads the corresponding part, and then "splice" the downloaded files according to the order of the original files

It's a complete document. This greatly improves the file download efficiency. For file download, multi-threaded download must be considered.

Multi thread download can be roughly divided into the following steps:

1、 Gets the size of the target file on the server

Obviously, this step is to access the network first. You only need to get the total size of the target file. The purpose is to calculate the download task that each thread should allocate.

2、 Create a file of the same size as the original file locally

Locally, you can create a file with the same size as the target file through RandomAccessFile. The API supports read and write operations anywhere in the file. This provides convenience for multi-threaded download. Each thread only needs to write data within the specified start and end footprints.

3、 Calculate the start position and end position of each thread download

We can treat the original file as a byte array, and each thread only downloads the part between the specified start position and the specified end position of the "array". In the first step, we already know the total length of the "array". Therefore, as long as we know the total number of open threads, we can calculate the range of each thread to download. Number of bytes to be downloaded per thread (blocksize) = total bytes (totalsize) / number of threads (threadcount). Assuming that threads are numbered in the order of 0,1,2,3... N, the range of files downloaded by the nth thread is:

Starting footmark StartIndex = n * blocksize.

End pin endindex = (n-1) * blocksize-1.

Considering that totalsize / threadcount may not be divisible, special treatment should be given to the last thread. The calculation formula of the starting pin of the last thread remains unchanged, but the ending pin is endindex = totalsize-1.

4、 Start multiple sub threads to download

Implement the read stream operation in the sub thread, and read conn.getinputstream() into RandomAccessFile.

5、 Record download progress

Create a temporary file for each individual thread to record the download progress of the thread. For a single thread, the number of bytes currently downloaded is recorded in the local file for each part of the data downloaded. In this way, if the download task terminates abnormally, the next time you restart the download, you can continue the last progress download.

6、 Delete temporary file

When multiple threads are downloaded, the last downloaded thread deletes all temporary files.

Android has an interface for good interaction with users. On the interface, let users input the original file address and the number of threads, and then click OK to start downloading. In order to enable users to clearly see the download progress of each thread, an equal number of progress bars are dynamically generated according to the number of threads. ProgressBar is a progress bar control that displays the progress of a task. There are two styles: one is circular, which is the system default. Because the specific progress value cannot be displayed, it is suitable for situations where you are not sure how long to wait; The other is a long bar. This kind of progress bar has two colors, and the highlighted color represents the total progress of task completion. For our download tasks, since the total task (bytes to be downloaded) is clear, and the currently completed task (bytes to be downloaded) is also clear, the latter is particularly suitable. In our requirements, the progressbar needs to be added dynamically according to the number of threads, and it is required to be long. Therefore, you can write the style of ProgressBar in the layout file in advance. Fill the layout when needed. The max attribute of ProgressBar represents its maximum scale value, and the progress attribute represents the current progress value. The method of use is as follows:

ProgressBar.setMax(int max); Sets the maximum scale value.

ProgressBar.setProgress(int progress); Set the current progress value.

Setting the maximum scale value and modifying the progress value to the progressbar can be operated in the child thread. It has been specially processed internally, so it is no longer necessary to send a message through the handler to the main thread to modify the progress.

Here is the multithreading in Android environment written by ourselves.

The layout of the multi-threaded download interface is as follows. The three progress bars represent the download progress of the three sub threads respectively.

The internal logic of multi-threaded download is as follows. In fact, this is already available at the beginning, but it is just the implementation of the code.

Finally, don't forget to add permissions. In this project, not only network access but also sdcard storage are used, so two permissions need to be added.

In addition, xutils can also realize multi-threaded download. Xutils is an open source free Android toolkit, and the code is hosted on GitHub. At present, xutils mainly has four modules: dbutils module, which is mainly used to operate the framework of database. Viewutils module can manage UI, resource and event binding through annotation. Httputils module provides convenient network access, breakpoint continuation and other functions. Bitmaputils module provides a powerful image processing tool. Here we only use the httputils tool in the simple and practical xutils tool. The use of third-party packages is relatively simple. Directly copy the jar package of xutils to the LIBS directory, and then add dependencies.

Next, you can use the function of httputils in xutils:

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>