使用Asyncronous方法的Java多线程

时间:2015-08-11 03:58:15

标签: java multithreading

我使用ThreadPool概念创建100个线程。但我通过1000封电子邮件清单作为清理的电子邮件。 一段时间后出现异常

  

ConnectionPoolTimeoutException:等待来自池的连接的超时

如何解决该错误:

这是我的代码:

  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();
        }

    }
}

0 个答案:

没有答案