当前位置 : 主页 > 编程语言 > java >

多线程同时下载网页中的所有图片,完成后返回

来源:互联网 收集:自由互联 发布时间:2021-06-28
DownloadImages.java /** * 处理内容中包含的图片信息 * @param content */private List handleImages(String articleUrl, Element content) { List newImgUrls = new Vector(); ExecutorService threadPool = Executors.newFixedThreadPool(MAX_
DownloadImages.java
/**
 * 处理内容中包含的图片信息
 * @param content
 */
private List
 
   handleImages(String articleUrl, Element content) {
    List
  
    newImgUrls = new Vector<>(); ExecutorService threadPool = Executors.newFixedThreadPool(MAX_IMAGE_NUM);//newCachedThreadPool(); Set
   
     imgUrls = new HashSet<>();//图片去重 content.select("img").forEach(elem -> imgUrls.add(elem.absUrl("src"))); imgUrls.forEach(imgUrl -> threadPool.execute(()-> { if(newImgUrls.size() < MAX_IMAGE_NUM) { String newUrl = downloadImageAndUploadToCloud(imgUrl, articleUrl); if (newUrl != null && newImgUrls.size() < MAX_IMAGE_NUM) newImgUrls.add(newUrl); } })); //等待所有线程执行完毕并返回结果 try { threadPool.shutdown(); threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { log.error("Download images interrupted", e); } threadPool.shutdownNow(); return newImgUrls; }
   
  
 
网友评论