业务是这样的:
背景:提供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; }
线程池的引入,提高了性能
|
分享到:
相关推荐
.......................................JAVA线程、线程池资料----下载不扣分,回帖加1分,欢迎下载,童叟无欺JAVA线程、线程池资料----下载不扣分,回帖加1分,欢迎下载,童叟无欺JAVA线程、线程池资料----下载不...
C++线程池c-thread-pool-master.zip
线程池原理-ThreadPoolExecutor源码解析 1.构造方法及参数 2.阻塞对列: BlockingQueue 3.线程工厂: DefaultThreadFactory 4.拒绝策略: RejectedExecutionHandler 5.执行线程 Executor
NULL 博文链接:https://shaojiashuai123456.iteye.com/blog/1415886
此文档是: 基于简单线程池概念的JAVA服务器端应用 附有连接ORACLE数据库等简单操作. 操作描述: 服务器启动后,会启动10个子线程运行.(配合客户端10个请求进行模拟,控制台输出模拟过程) 服务器主程序进入一个有...
NULL 博文链接:https://lhgyy00.iteye.com/blog/450628
一个c实现的线程池threadpool-0.1,有测试代码 This is an implementation for the following requirements: http://paul.rutgers.edu/~csgates/CS416/proj2/index.html Reports of successful use are ...
NULL 博文链接:https://lhgyy00.iteye.com/blog/452050
NULL 博文链接:https://x125858805.iteye.com/blog/2191873
1. 初始化线程执行提交的任务 2. 任务执行不过来,放入工作队列 3. 任务过多,线程和队列均处理不过来,拒绝执行(本文中将抛出RejectedExecuti
没有用并发包里面的锁的支持,便于了解监视器类里面的线程协调机制
// 如果实际线程数量小于核心线程数,if (getWorkCount() )// 初始化线程执行任务else if (work
博文地址:http://blog.csdn.net/csnd_ayo/article/details/72457190
C++多线程从原理到线程池实战是一门针对C++程序员设计的课程。通过深入学习多线程的原理和技术,学生将掌握线程管理、同步与...课程还包括线程池的设计与实践,帮助学生提高程序性能和并行处理能力。 视频大小:989M
线程是系统中可执行调度的...线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,规避资源耗尽的风险。集合的详细描述,以及集合中的异同点,HashMap不同jdk版本区别,ConcurrentHashMap介绍。
maven-text-demo 练习使用 反射- 注解 -过滤器-线程池--设计模式
两种线程池的实现和性能评价 计算机工程与设计
Java并发编程:线程池的使用 - 平凡希 - 博客园平凡希博客园首页联系管理随笔 - 127 文章 - 1 评论 - 94Java并发编程:线程池的使用在前面
Demo-实例讲解线程池里面的UI如何刷新,处理两个开发者头疼的问题: 1. 数据经常需要读取更新,并且比较耗时,需要分步刷新UI. 2. UI界面切换后,如何停止掉子线程里面正在读取的数据而不会将旧数据刷新到新UI界面上...
Socket网络编程学习笔记之---使用线程池提高性能