使用CompletionService实现页面渲染器

public class Render{
    
    
  private final ExecutorService executor;
  Render(ExecutorService executor){
    
    this.executor = executor}

  void renderPage(CharSequence source){
    
    
    List<ImageInfo> info = scanForImageInfo(source);
    CompletionService<> completionService = new ExecutorCompletionService<ImageData>(executor);
    for(final ImageInfo imageInfo:info){
    
    
      completionService.submit(new Callable<ImageData>(){
    
    
       public ImageData call(){
    
    
         return imageInfo.downloadImage();
       }
     });
     renderText(source);

     try{
    
    
       for(int t=0;t< n.size();t++){
    
    
         Future<ImageData> f = completionService.take();
         ImageData imageData = f.get();
         renderImage(imageData);
       }
     }catch(InterruptedException e){
    
    
       Thread.currentThread().interrupt();
     }catch(ExecutionException e){
    
    
       throw launderThrowable(e.getCause());
     }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/118229047