title: Different connection speed to URL depending on number of threads
body: <p>I need your advice. I'm trying to make some sort of code that will bulk check av. connection speed to URLs in multiple threads. Code works. But I don't understand why when I'm using 4 threads the av.connection speed  is say 150ms and using 20 threads it can raise up to 500-600ms ? Any clues?</p>
  
  <p>The idea of how the code below should work: In main I start a "ThreadManager" that starts multiple Threads that will establish connections with URLs. URLs are kept in a synchronized ArrayList, and the results are also written in a synchronized ArrayList.</p>
  
  <pre><code>package URLConnectionSpeedChecker; 
  public class ThreadManager implements Runnable{
  
  private static Base base;
  
  @Override
  public void run() {
      base = new Base();
      Thread[] threads = new Thread[4];
      for (int i = 0; i &lt; 4; i++ ) {
          CheckerThread checkerThread = new CheckerThread(base);
          Thread thread = new Thread(checkerThread);
          threads[i] = thread;
      }
      for (Thread x : threads) {
          x.start();
      }
      for (Thread x : threads) {
          try {
              x.join();
  
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  public static Base getBase (){
      return base;
  }
  </code></pre>
  
  <p>The code that is run in a thread:</p>
  
  <pre><code>package URLConnectionSpeedChecker;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.SocketTimeoutException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.ArrayList;
  
  public class CheckerThread implements Runnable {
  
  private static Base base;
  
  CheckerThread(Base base) {
      this.base = base;
  }
  
  @Override
  public void run() {
  
      while (base.getIsOutlistOfURLs() == false) {
  
          long timeDifference = 0;
  
          try {
              timeDifference = connectTime(base.getListOfUrls());
          } catch (SocketTimeoutException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          addTimeDifference(base.getListOfTimeDifferences(), timeDifference);
      }
  }
  
  
  public long connectTime(ArrayList&lt;String&gt; list) throws IOException {
      long startTime = 0;
      long finishTime = 0;
      long timeDifference;
  
      URL url = getURL(list);
      if (!(url == null)) {
          startTime = System.currentTimeMillis();
          URLConnection urlConnection = url.openConnection();
          urlConnection.setConnectTimeout(3000);
          urlConnection.connect();
          finishTime = System.currentTimeMillis();
          timeDifference = finishTime - startTime;
          return timeDifference;
      } else {
          return 0;
      }
  }
  
  synchronized public void addTimeDifference(ArrayList&lt;Long&gt; list, long timeDifference) {
      list.add(timeDifference);
  }
  
  synchronized public URL getURL(ArrayList&lt;String&gt; list) {
      if (list.size() &gt; 0) {
  
          if (list.get(0).length() &gt; 0) {
              URL url = null;
              try {
                  url = new URL(list.get(0));
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              }
              list.remove(0);
              return url;
          } else if (list.get(0).length() &lt; 0) {
              list.remove(0);
              return null;
          } else {
              return null;
          }
      } else {
          base.setIsOutlistOfURLs();
          return null;
      }
  }
  }
  
  
  }
  </code></pre>