title: How to run multiple get requests from one thread in java?
body: <p>I want to set up a client with x connections, where each connection will send a new GET request to a different URL each random time.</p>
  
  <p>So my approach is to create x threads, where each will send a GET request in a loop (till the program will terminate).
  I was using Apache MultiThreadedHttpConnectionManager, where I set the number of possible connections as x. </p>
  
  <pre><code>
      ClientHandler(int numberOfClients) {
          this.numberOfClients = numberOfClients;
  
          connManager = new PoolingHttpClientConnectionManager();
          HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(connManager);
  
          connManager.setMaxTotal(numberOfClients);
          CloseableHttpClient httpClient = clientBuilder.build();
  
          generateMultiThreadedClient(httpClient);
      }
  
      private void generateMultiThreadedClient(CloseableHttpClient httpClient) {
  
          for (int i = 0; i &lt; numberOfClients; i++) {
              String clientUrl = URL + i;
              HttpGet httpGet = new HttpGet(clientUrl);
  
              ClientMultiThreaded clientMultiThreaded = new ClientMultiThreaded(httpClient,httpGet, i);
              LOGGER.info(CLIENT_LOG + "A new thread for clientId " + i + " was created.");
  
              clientMultiThreaded.start();
              LOGGER.info(CLIENT_LOG + "Thread for clientId " + i + " started..");
  
          }
      }
  </code></pre>
  
  <p>And this is my run() method of the thread. </p>
  
  <pre><code>ClientMultiThreaded(CloseableHttpClient httpClient, HttpGet httpGet, int clientId) {
          this.httpClient = httpClient;
          this.clientId = clientId;
          this.httpGet = httpGet;
          randomGenerator = new Random();
      }
  
      @Override
      public void run() {
          try{
              // Execute request for the first time
              executeRequest(httpGet);
  
              while (true) {
                  int timeToSleep = randomGenerator.nextInt(BOUND_LIMIT) + 1;
                  LOGGER.info(CLIENT_LOG + "Thread id " + clientId + " went to sleep for " + timeToSleep / 1000 + " seconds");
                  sleep(timeToSleep);
  
                  LOGGER.info(" ------- This is a test log printing for clientId: " +clientId);
                  executeRequest(httpGet);
              }
          }catch(Exception e) {
              LOGGER.info(e.getMessage());
          }
      }
  
      private void executeRequest(HttpGet httpGet) throws IOException {
          CloseableHttpResponse response = httpClient.execute(httpGet);
          LOGGER.info(CLIENT_LOG + "clientId " + clientId + " sent get request to URL " + httpGet.getURI());
  
          statusCode = this.response.getStatusLine().getStatusCode();
          LOGGER.info(CLIENT_LOG + "Status received for clientId " + clientId + " is: " + statusCode);
      }
  </code></pre>
  
  <p>The problem is that I actually get 2 GET requests sent and then it stops.</p>