title: Invalid use of BasicClientConnManager: connection still allocated
body: <p>I am making a call to REST URL and trying to measure how much time it is taking to get the response back.</p>
  
  <p>I am using <code>DefaultHttpClient</code> for that to get the response back from <code>REST URL</code>.</p>
  
  <p>In my below program , each thread will be working on a particular range. Like Each Thread will work between <code>1 - 100</code> and second thread will work between <code>101 - 200</code> etc.</p>
  
  <p>SO in my below code, for the first time it works fine. But for the second time, it is throwing exception on this line <code>httpclient.execute</code> for the second time as-</p>
  
  <pre><code>java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
  Make sure to release the connection before allocating another one.
  </code></pre>
  
  <p>Is there anything wrong I am doing here?-</p>
  
  <p>Below is my code-</p>
  
  <pre><code>class Task implements Runnable {
  
      private DefaultHttpClient httpclient = new DefaultHttpClient();
      private HttpGet httpGet;
      private HttpResponse response;
  
      @Override
      public void run() {
  
          try {
  
              httpGet = new HttpGet(
                      "http://localhost:8080/service/BEService/v1/get/USERID=10000/profile.ACCOUNT.SERVICE
              httpGet.getRequestLine();
  
              for (int userId = id; userId &lt; id + noOfTasks; userId++) {
  
                      long start = System.nanoTime();
  
                      response = httpclient.execute(httpGet);
  
                      long end = System.nanoTime() - start;
                  }
          } catch (Exception e) {
              LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
          }
      }
  }
  </code></pre>
  
  <p><strong>Updated Code:-</strong></p>
  
  <p>If I am doing it something like this-</p>
  
  <pre><code>class Task implements Runnable {
  
      private DefaultHttpClient httpclient = new DefaultHttpClient();
      private HttpGet httpGet;
      private HttpResponse response;
  
      @Override
      public void run() {
  
          try {
  
              for (int userId = id; userId &lt; id + noOfTasks; userId++) {
  
                  httpGet = new HttpGet("http://localhost:8080/service/BEService/v1/get/USERID=10000/profile.ACCOUNT.SERVICE");
                  httpGet.getRequestLine();
  
                  long start = System.nanoTime();
  
                  response = httpclient.execute(httpGet);
  
                  long end = System.nanoTime() - start;
  
                  HttpEntity entity = response.getEntity();
                  EntityUtils.consume(entity);
                  }
          } catch (Exception e) {
              LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
          }
      }
  }
  </code></pre>
  
  <p>then it is fine or not?</p>