1. 首页
  2. >
  3. 编程技术
  4. >
  5. Java

使用HttpClient多线程收集数据

public Map<String, String> main(){      try {     PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();     CloseableHttpClient httpClient = HttpClients.custom()             .setConnectionManager(cm)             .build();      Map<String, String> urlMap = new HashMap<String, String>();     urlMap.put("test1", "https.test1.com");     urlMap.put("test2", "https.test2.com");      List<GetThread> threads = new ArrayList<>(urlMap.size());     Map<String, String> contentMap = new HashMap<>();     for(Map.Entry<String, String> entry : urlMap.entrySet()) {         threads.add(new GetThread(httpClient, new HttpGet(entry.getValue()), entry.getKey(), contentMap);     }      for(Thread thread : threads) {         thread.start();     }          for(Thread thread : threads) {             thread.join();             }     return contentMap;     } catch (InterruptedException e) {         e.printStackTrace();     }          return Collections.emptyMap(); }    class GetThread extends Thread {     private final CloseableHttpClient httpClient;     private final HttpGet httpGet;     private final String urlType;     private Map<String, String> contextMap;      public GetThread(CloseableHttpClient httpClient, HttpGet httpGet, String urlType, Map<String, String> contextMap) {         this.httpClient = httpClient;         this.httpGet = httpGet;         this.urlType = urlType;         this.contextMap = contextMap;     }      @Override    public void run(){         CloseableHttpResponse response = null;         try {             response = httpClient.execute(httpGet);             String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);             contextMap.put(urlType, responseBody);         } catch (IOException e) {             e.printStackTrace();         }finally {             if(null != response){                 try {                     response.close();                 } catch (IOException e) {                     e.printStackTrace();                 }             }         }     } }