0

I am using ThreadPool concept to create 100 Threads. But i am passing List of 1000 emails as cleaned Emails. After sometime Exception comes

ConnectionPoolTimeoutException: Timeout waiting for connection from pool

How can i resolve that error:

Here is my code:

  ExecutorService executor = Executors.newFixedThreadPool(100);

    for (String email:cleanedEmails) {  
        Runnable worker = new MyRunnable(email);
        executor.execute(worker);
    }
    executor.shutdown();
    // Wait until all threads are finish
    while (!executor.isTerminated()) {

    }
    System.out.println("\nFinished all threads");

}

public static class MyRunnable implements Runnable {
    private final String email;

    MyRunnable(String email) {
        this.email = email;
    }

    public void run() {

        DomainDetails obj=null;
        try 
        {
            HttpResponse<JsonNode> response = Unirest.get("XXXXX")
                .header("X-Mashape-Key", "XXXXXX")
                .header("Accept", "application/json")
                .asJson();

            String response1=response.getBody().toString();

             obj=new ObjectMapper().readValue(response1, DomainDetails.class);
             System.out.println(email+ "\t\tExist:" +obj.getExist() );

            } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
anand mishra
  • 33
  • 1
  • 1
  • 7
  • Increase the timeout, or use fewer threads. – user207421 Aug 11 '15 at 10:42
  • I am not setting anywhere timeout. Also I have to validate more data so i need more threads. – anand mishra Aug 11 '15 at 10:53
  • 1
    doing while (!executor.isTerminated()); is a VERY bad idea. Replace it with something like if (!executor.awaitTermination(1, TimeUnit.HOURS)) { //Something clearly went wrong } – David ten Hove Aug 11 '15 at 11:00
  • If i am using only 10 threads then it is working fine. But i need to complete my tasl faster so i need to use more threads. If i am using more threads then Exception came:org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool – anand mishra Aug 12 '15 at 04:23

0 Answers0