title: Java 9 Multithreaded HTTP request
body: <p>I can't figure out how to covert the following sequential code to a multi-threaded properly. I have done my best to avoid any shared resource and complete thread independence.</p>
  
  <p>This is the <strong>single-threaded</strong> code 
      package com.net;</p>
  
  <pre><code>import jdk.incubator.http.HttpClient;
  import jdk.incubator.http.HttpRequest;
  import jdk.incubator.http.HttpResponse;
  import org.jsoup.Jsoup;
  import org.jsoup.nodes.Document;
  
  import java.io.IOException;
  import java.net.URI;
  import java.net.URISyntaxException;
  
  public class ReqSingle {
  
      private float totalElapsedTime = 0F;
      private HttpClient client = HttpClient.newHttpClient();
  
      public ReqSingle(String[] sties){
          for (String site : sties){
              long fetchStartTime = System.currentTimeMillis();
              String html = getResource(site);
              float elapsed = (float) (System.currentTimeMillis() - fetchStartTime) / 1000;
  
              Document doc = Jsoup.parse(html);
              System.out.println("It took " + elapsed + " seconds to fetch " + site + " with title " + doc.title());
              totalElapsedTime += elapsed;
          }
  
          System.out.println("Total Elapsed Time: " + totalElapsedTime + "\nTotal Number of sites: " + sties.length);
      }
  
      private String getResource(String someUrl) {
          String body = "";
          try {
              URI url = new URI(someUrl);
              HttpRequest request = HttpRequest.newBuilder()
                      .uri(url).GET().build();
              HttpResponse&lt;String&gt; response = client.send(request, HttpResponse.BodyHandler.asString());
              body = response.body();
          } catch (URISyntaxException e) {
              System.out.println("URL " + someUrl + "is not valid");
          } catch (IOException | InterruptedException e) {
              // do nothing
          }
          return body;
      }
  }
  </code></pre>
  
  <p>This is my implementation of <strong>multi-threaded</strong> code:</p>
  
  <pre><code>package com.net;
  
  import jdk.incubator.http.HttpClient;
  import jdk.incubator.http.HttpRequest;
  import jdk.incubator.http.HttpResponse;
  import org.jsoup.Jsoup;
  import org.jsoup.nodes.Document;
  
  import java.io.IOException;
  import java.net.URI;
  import java.net.URISyntaxException;
  
  public class ReqThreaded {
  
      public ReqThreaded(String[] sites) {
  
          for (String rawUri : sites) {
              new Thread(new Site(rawUri)).start();
          }
      }
  
      class Site implements Runnable {
          URI uri;
  
          public Site(String rawUri) {
              try {
                  uri = new URI(rawUri);
              } catch (URISyntaxException e) {
                  System.out.println("URL " + rawUri + "is not valid");
              }
          }
  
          @Override
          public void run() {
              String html = "";
  
              long fetchStartTime = System.currentTimeMillis();
              try {
                  HttpClient client = HttpClient.newHttpClient();
                  HttpRequest request = HttpRequest.newBuilder()
                          .uri(uri).GET().build();
                  HttpResponse&lt;String&gt; response = client.send(request, HttpResponse.BodyHandler.asString());
                  html = response.body();
              } catch (IOException | InterruptedException e) {
                  //do nothing
              }
              float elapsed = (float) (System.currentTimeMillis() - fetchStartTime) / 1000;
              Document doc = Jsoup.parse(html);
              System.out.println("It took " + elapsed + " seconds to fetch " + uri.toString() + " with title " + doc.title());
          }
      }
  }
  </code></pre>
  
  <p>And this is the tester class and it's output:</p>
  
  <pre><code>package com.net;
  
  public class ReqTester {
      public static void main(String[] args) {
  
          String[] topIranianSites = {
                  "https://www.aparat.com/",
                  "http://www.varzesh3.com/",
                  "http://namnak.com/",
                  "http://www.telewebion.com/",
                  "https://divar.ir/",
                  "https://www.ninisite.com/",
                  "https://www.blogfa.com/",
                  "http://www.namasha.com/",
                  "http://www.yjc.ir/"
          };
  
          System.out.println("Single Threaded Test Results:\n");
          new ReqSingle(topIranianSites);
          System.out.println("\n\nMulti Threaded Test Results:\n");
          new ReqThreaded(topIranianSites);
  
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>WARNING: Using incubator modules: jdk.incubator.httpclient
  Single Threaded Test Results:
  
  It took 2.843 seconds to fetch https://www.aparat.com/ with title آپارات - سرویس اشتراک ویدیو
  It took 0.459 seconds to fetch http://www.varzesh3.com/ with title 
  It took 0.52 seconds to fetch http://namnak.com/ with title نمناک
  It took 2.231 seconds to fetch http://www.telewebion.com/ with title تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
  It took 0.243 seconds to fetch https://divar.ir/ with title 
  It took 1.749 seconds to fetch https://www.ninisite.com/ with title نی نی سایت | راهنمای بارداری و بچه داری
  It took 0.407 seconds to fetch https://www.blogfa.com/ with title 
  It took 1.796 seconds to fetch http://www.namasha.com/ with title نماشا - سرویس رایگان اشتراک ویدیو
  It took 0.232 seconds to fetch http://www.yjc.ir/ with title 
  Total Elapsed Time: 10.48
  Total Number of sites: 9
  
  
  Multi Threaded Test Results:
  
  It took 0.114 seconds to fetch https://divar.ir/ with title 
  It took 0.236 seconds to fetch http://www.yjc.ir/ with title 
  It took 1.519 seconds to fetch http://www.varzesh3.com/ with title 
  It took 1.587 seconds to fetch https://www.blogfa.com/ with title 
  It took 3.524 seconds to fetch http://namnak.com/ with title نمناک
  It took 4.152 seconds to fetch https://www.ninisite.com/ with title نی نی سایت | راهنمای بارداری و بچه داری
  It took 4.486 seconds to fetch http://www.namasha.com/ with title نماشا - سرویس رایگان اشتراک ویدیو
  It took 4.725 seconds to fetch https://www.aparat.com/ with title آپارات - سرویس اشتراک ویدیو
  It took 5.82 seconds to fetch http://www.telewebion.com/ with title تلوبیون | مرجع پخش زنده و دانلود فیلم ، سریال و سایر برنامه های تلویزیون
  </code></pre>