• Just learned the Java multithreaded foundation and computer network and so on, no practice can not line, try to write a Java multithreaded download to play

Does anyone have a problem with me saying my wife is on the cover?


Preliminary principle learning and project conception

Catharsis of basic concepts
At first sightBreakpoint continuation multithreaded downloaderWhen the question?

I mean, it’s not unusual for this thing to have a lot of problems at first glance, and it’s not just my food. We usually the most commonly used download should be thunderbolt, or is directly downloaded on the browser. If I had to build my own downloader, my first reaction would probably be a demo of a transport tool. Thus, the following problems arise:

  • What is breakpoint continuation?

  • What does a multithreaded downloader mean?

  • What’s better than multithreaded and single-threaded downloads?

  1. Breakpoint continuation is what

    • FTP (File Transfer Protocol) (File Transfer Protocol), FTP client software breakpoint continuingly refers to the download or upload [] (https://baike.baidu.com/item/ upload / 749624), To download or upload task (a file or a [package] (5373066) zip/https://baike.baidu.com/item/) artificially divided into several parts, Each part adopts a [] thread thread (https://baike.baidu.com/item/ / 103101) to upload or download, if [network] (https://baike.baidu.com/item/ network fault / 1391028), Can start from have already upload or download section to upload download unfinished part, and there is no need to upload/download from scratch (https://baike.baidu.com/item/ / 2270927). Users can save time and speed up.

      — A section of Baidu Baike for you

    • In plain English, you stop when I tell you to, and the next time I tell you to continue, you’ll have to pick up where you left off. This should be the most basic quality of a downloader

  2. What does a multithreaded downloader mean?

    • From our intuitive point of view, we have a progress bar when we download, and we might think that there’s only one channel for downloading files, but there’s probably not,The concept of threads and processesI won’t repeat it here. At download time, the downloader will split a file into several pieces, each for a thread to download, does this feel a lot faster?
    • So, if I start N threads, is it N times faster than a single thread? Isn’t the more threads the better? In fact, more threads are not always better, and N threads are not N times faster than a single thread. Basically, thread synchronization is just a macro view. From the CPU’s point of view, it is nothing more than multiple threads taking turns occupying CPU resources in a planned slice of time. It’s not really going hand in hand. Which brings us to the next question:
  3. What’s better than multithreaded and single-threaded downloads?

    • Multithreaded cock is still cock, at least according to the upperclassman, I don’t care, I just study where it is cock. Since we say that multithreading is fast, it’s not so fast that it can really go hand in hand, so where is it fast? Here is a knowledge point involved => congestion avoidance algorithm

      Congestion avoidance algorithm:

      Huh? About this knowledge point, I made a note of, lake university teacher forever god.

      Congestion avoidance algorithms link: juejin.cn/post/694580…

    • So, if a single thread is jammed and the algorithm gets stuck, the overall download speed is really slashed. Eggs should not be put in the same basket, if we open multithreading, as long as not all processes are cut, it will be faster than a single thread cut.

    • Of course, if there is no sending IO blocking at all during the entire download process, then I think single-threaded is still faster. After all, multi-threaded context switching is inefficient.

The implementation principle of breakpoint continuation

Well, now that we know that breakpoint continuation seems to be the real deal, it’s time to think about how to implement multithreaded breakpoint continuation. Therefore, a new question has been raised:

  • How do I know if this download link will give me a breakpoint to continue?

  • How do I split a file to multiple threads?

  • If I upload half of the file and it updates after a while, what happens?

  • I know I can resume it, I know if the file is updated, and THEN I download it with multiple processes, and then what?


  1. How do I know if this download link will give me a breakpoint to continue?

    • First, let’s take a look at the HTTP protocol. HTTP has been supporting partial retrieval of files since 1.1, which provides support for parallel downloads and resumable breakpoints.
    • There is a request header in this HTTP requestRequest headerAnd the response headersResponse header. If there are any in the response headerAccept-ranges: bytes // Whether transmission Ranges are allowed to be specified. Bytes: Ranges are requested in bytes, and None: no range requests are supportedThis field, well, you can continue from a breakpoint.Where to get down, where to get up, or else, start all over again, just like a video game was deleted.
  2. How do I split a file to multiple threads?

    • We can have a look atContent-RangeContent-range: a-b indicates that bytes are downloaded from bytes A to bytes B
  3. If I upload half of the file and it updates after a while, what happens?

    • For this question, we can have two key words:ETagandLast-Modified.
    • Last-ModifiedYou can record the last updated timestamp of the file to judge whether the file has been modified. Some files may be modified periodically, some files may be modified too fast, and some servers may have inaccurate time records. Therefore, this field does not seem to be able to completely prove that the content of the file has been modified.
    • ETagThe HTTP/1.1 standard does not specify what an Etag is or how it should be implemented. The only stipulation is that the Etag should be placed within “”.
  4. I know I can resume it, I know if the file is updated, and THEN I download it with multiple processes, and then what?

    • You can then use RandomAccessFile to combine the downloaded temporary files into a single file, and the first version of the project appears

    I’ve studied the basic process, not a word of the code. Is there a big man to take me — 2021.4.4