技能库--统计文件大小 + ExchangeData+BlockingQueue(89)
来源:互联网 发布:php image not loaded 编辑:程序博客网 时间:2024/06/04 19:24
主线程负责不断的汇总结果,pool线程池负责explore每个文件或者目录,并不断的put结果到线程交换数据的–BlockingQueue
package com.qunar.finance.paper.countFileSize;import java.io.File;import java.util.concurrent.*;import java.util.concurrent.atomic.AtomicLong;public class ExchangeData { private ExecutorService service; final private BlockingQueue<Long> fileSizes = new ArrayBlockingQueue<Long>(1000); final AtomicLong pendingFileVisits = new AtomicLong(); private void startExploreDir(final File file) { pendingFileVisits.incrementAndGet(); service.execute(new Runnable() { @Override public void run() { exploreDir(file); } });// System.out.println(Thread.currentThread().getName()+"--提交任务"); //该线程任务结束 } private void exploreDir(File file) { long fileSize = 0; if (file.isFile()) { fileSize = file.length(); } else { File[] children = file.listFiles(); if (children != null) { for (File child : children) { if (child.isFile()) { fileSize += child.length(); }else{//生成调度任务 生成子任务 -- 下一步 local thread 任务结束 startExploreDir(child); } } } } try { fileSizes.put(fileSize); } catch (InterruptedException e) { e.printStackTrace(); } pendingFileVisits.decrementAndGet();// System.out.println(Thread.currentThread().getName()+"--干完了"+file.getName()); } private long getTotalSizeOfFile(final String fileName) throws InterruptedException{ service = Executors.newFixedThreadPool(100); try { startExploreDir(new File(fileName)); long totalSize = 0; while (pendingFileVisits.get() > 0 || fileSizes.size() > 0) { final Long size = fileSizes.poll(10, TimeUnit.SECONDS);// System.out.println(Thread.currentThread().getName()+"--polling"); totalSize += size; } return totalSize; } finally { service.shutdown(); } } public static void main(final String[] args) throws InterruptedException { final long start = System.nanoTime(); final long total = new ExchangeData().getTotalSizeOfFile("C:\\Users"); final long end = System.nanoTime(); System.out.println("Total Size: " + total); System.out.println("Time taken: " + (end - start)/1.0e9); }}
运行结果: 提高到了20秒内
Total Size: 50850938212Time taken: 16.281116711Process finished with exit code 0
0 0
- 技能库--统计文件大小 + ExchangeData+BlockingQueue(89)
- 技能库--统计文件大小 + CountDownLatch(87)
- 技能库--统计文件大小 +Fork-Join(90)
- 技能库--统计文件大小 + Executors+Future(86)
- 统计目录和文件大小
- android统计文件大小实例
- 批处理统计文件大小
- 递归统计文件大小
- 【python技能】词频统计
- linux统计文件大小和数量
- hdfs统计行数和统计文件大小
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- 测试oracle数据库是否连通测试和忘记管理员密码找回命令
- 解决UbuntuServer1.14.10安装build-essential等软件依赖libc6报错“The following packages have unmet dependencies” |
- Hash Algorithm Identifier:一款快速识别Hash加密类型的工具
- Controller参数绑定
- @RequestMapping
- 技能库--统计文件大小 + ExchangeData+BlockingQueue(89)
- controller方法返回值
- SpringMVC中@RequestMapping详解
- 聊聊对Android视图滚动状态的监听
- 纯代码给WordPress网站添加独立下载页面功能
- 详细图解MySQL(win7x64 5.7.16版本)下载、安装、配置与使用
- sql语句在遇到字符串的时候,使用单引号
- ℃江让您从精通到入门:App登陆模块的实现
- java 调用mvel; 文件或字符串