title: How to handle ThreadPool hitting the server connection limit
body: <p>I could be asking a design pattern question here.</p>
  
  <p>On Android, I am using a thread pool to open 8 threads to download some files.</p>
  
  <pre><code>    try {
          ExecutorService pool = Executors.newFixedThreadPool(8);
          for (int i = 0; i &lt; someList.size(); i++) {
              pool.submit(new DownloadJsonTask(someList.get(i), context));
          }
          pool.shutdown();
          pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
      } catch (Exception e) {
      }
  </code></pre>
  
  <p>I noticed if I use one thread to download one-by-one, then I hardly get download fails but if I use 8 threads, then I sometimes get download fails. I am not a server/network person so I don't know in detail but I am guessing the server is putting a limit of one device (or one IP address) trying to connect multiple connections.
  <br><br>If this is the reason, then how do I design the code to overcome this issue? I have already implemented to try downloading 3 times before failing. It does seem to fixed it "for now". However, I know my code is not robust and it can fail at one point.</p>
  
  <p>I figured, I wouldn't be the first one facing this issue. I would like to know a robust solution around this issue.</p>
  
  <p><strong>Solutions I could think of:</strong><br>
  - Try to download at least 3 times before failing<br>
  - Once failed, then try to sleep for a random amount of time. So that failed threads don't wake up at the same time and fail again.<br>
  - If the server throws some kind of unique message back such as Server busy, then re-try unlimited(?)(large amount of) times.</p>
  
  <p>I have not yet implemented above possible solutions. I want to know the common/best solution first and spend time implementing it.</p>
  
  <p>Any ideas?</p>