`

线程池的引入-解决性能问题

 
阅读更多

业务是这样的:

背景:提供rsf服务

1.先根据图片流,将图片写入到服务端的/dev/shm文件下下面(此路径问服务器端内存目录);

2.利用gm将服务器端的图片锐化并变灰;

3.再利用tesseract将图片识别并形成txt文件;

4.由于图片和txt文件均为临时文件,便利用线程池的方式将临时文件删除

private static final Log  logger = LogFactory.getLog(IdentifyImgServiceImpl.class);
 
 ExecutorService es = Executors.newSingleThreadExecutor();
 
 
 class DeleteFile implements Runnable{
  //源文件路径
  private String originalPath;
  //锐化变灰后路径
  private String sharpenPath;
  //识别后文本路径
  private String resultPath; 
  
  /**
   * 删除产生的临时文件
   * @param originalPath
   * @param sharpenPath
   * @param resultPath
   */
  DeleteFile(String originalPath,String sharpenPath,String resultPath){
   this.originalPath = originalPath;
   this.sharpenPath = sharpenPath;
   this.resultPath = resultPath;
  }
  @Override
  public void run() {
   File oFile = new File(this.originalPath);
   File sFile = new File(this.sharpenPath);
   File rFile = new File(this.resultPath);
   if(oFile.exists() && oFile.isFile()){
    oFile.delete();
   }
   if(sFile.exists() && sFile.isFile()){
    sFile.delete();
   }
   if(rFile.exists() && rFile.isFile()){
    rFile.delete();
   }
  }
 }
 
 /**
  * 识别图片
  */
 @Override
 public List<String> identifyImgNum(boolean sharpen,List<byte[]> originalByteList) throws Exception{
  long startTime = System.currentTimeMillis();
  List<String> reList = new ArrayList<String>();
  try {
   Runtime rt = Runtime.getRuntime();
   for (int i = 0; (originalByteList != null && originalByteList.size() > 0) && i < originalByteList.size(); i++) {
    long beginTime1 = System.currentTimeMillis();
    //UUID uuid = UUID.randomUUID();
    //String fileName = uuid.toString();
    // 获得当前时间
    DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    // 转换为字符串
    String formatDate = format.format(new Date());
    // 随机生成文件编号
    int random = new Random().nextInt(10000);
    String fileName = new StringBuffer().append(formatDate).append(random).toString();
    System.out.println("临时文件名=============" + fileName);
    String originalPath = "D:/dev/shm/"+fileName+".jpg";
    String sharpenPath = "D:/dev/shm/"+fileName+".tif";
    String resultPath = "D:/dev/shm/result"+fileName;
    File originalFile = new File(originalPath);
    OutputStream out = new FileOutputStream(originalFile);
    out.write(originalByteList.get(i));
    out.flush();
    out.close();
    Process proc = null;
    if(sharpen && (originalFile.isFile() && originalFile.exists())){
     //锐化并变灰
     proc = rt.exec("gm convert -sharpen 10x10 -compress none -depth 8  -colorspace Gray  " + originalPath +" "+ sharpenPath);
     proc.waitFor();
     long endTime1 = System.currentTimeMillis();
     logger.info("endTime1 - beginTime1=" + (endTime1 - beginTime1));
    }else if(originalFile.isFile() && originalFile.exists()){
     long endTime2 = System.currentTimeMillis();
     //变灰
     proc = rt.exec("gm convert -sharpen 10x10 -compress none -depth 8  -colorspace Gray  " + originalPath +" "+ sharpenPath);
     proc.waitFor();
     logger.info("endTime2 - beginTime1=" + (endTime2 - beginTime1));
    }else{
     throw new Exception("源文件读取失败");
    }
    long endTime3 = System.currentTimeMillis();
    //识别
    logger.info("tesseract "+sharpenPath+" "+resultPath);
    proc = rt.exec("tesseract  "+sharpenPath+" "+resultPath);
    proc.waitFor();
    long endTime4 = System.currentTimeMillis();
    logger.info("endTime4 - endTime3=" + (endTime4 - endTime3));
    String encoding="GBK";
             File inFile=new File(resultPath + ".txt");
             if(originalFile.isFile() && originalFile.exists()){ //判断文件是否存在
                 InputStreamReader read = new InputStreamReader(
                 new FileInputStream(inFile),encoding);//考虑到编码格式
                 BufferedReader bufferedReader = new BufferedReader(read);
                 String lineTxt = null;
                 while((lineTxt = bufferedReader.readLine()) != null){
                  if(lineTxt != null && !"".equals(lineTxt)){
                   reList.add(lineTxt);
                  }
                 }
                 read.close();
                 long endTime5 = System.currentTimeMillis();
                 logger.info("endTime5 - endTime4=" + (endTime5 - endTime4));
                 //提交给多线程
                 long endTime6 = System.currentTimeMillis();
                es.submit(new DeleteFile(originalPath,sharpenPath,resultPath + ".txt"));
                 long endTime7 = System.currentTimeMillis();
                 logger.info("endTime7 - endTime6=" + (endTime7 - endTime6));
         }else{
             throw new Exception("找不到指定文件");
         }
     }
  } catch (Exception e) {
   logger.error("IdentifyImgServiceImpl:identifyImgNum()", e);
  }
  long endTime = System.currentTimeMillis();
  logger.fatal("IdentifyImgServiceImpl identifyImgNum() cost time:" + (endTime - startTime));
  return reList;
 }

 

线程池的引入,提高了性能

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics