从Java读取unix cgroup文件的最快方法是什么?

从Java读取unix cgroup文件的最快方法是什么?,java,file,optimization,Java,File,Optimization,在Java中运行资源密集型计算时,我想阅读cca的内容。cgroup文件夹中的10-15个文件(如cpuacct.usage\u user,或来自blkio的文件) 我创建了一个collectRunnable对象,它将由SchedulerExecutorService每隔5秒执行一次。 我应该如何实现读取算法以最小化文件读取的开销 public void collectData() { Runnable collect = () -> { Strin

在Java中运行资源密集型计算时,我想阅读cca的内容。
cgroup
文件夹中的10-15个文件(如
cpuacct.usage\u user
,或来自
blkio
的文件)

我创建了一个
collect
Runnable对象,它将由
SchedulerExecutorService
每隔5秒执行一次。 我应该如何实现读取算法以最小化文件读取的开销

public void collectData() {
        Runnable collect = () -> {
            String[] filePaths = {""}; // assume it's not empty
            String[] fileContent = new String[filePaths.length];

            for (int i = 0; i < filePaths.length; i++) {
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(new File(filePaths[i])));

                    StringBuilder sb = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }

                    fileContent[i] = sb.toString();
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        executorService.scheduleAtFixedRate(collect, 0, 5, TimeUnit.SECONDS);
    }
public void collectData(){
可运行收集=()->{
String[]filepath={”“};//假设它不是空的
String[]fileContent=新字符串[filepath.length];
for(int i=0;i
您可以在
文件路径上使用
数组.stream
forEach
来收集内容。另外,我们需要更高范围的
StringBuilder
来实际收集行。像

StringBuilder sb = new StringBuilder();
Runnable collect = () -> {
    String[] filePaths = { "" }; // assume it's not empty
    String[] fileContent = new String[filePaths.length];
    Arrays.stream(filePaths).forEach(fp -> {
        List<String> lines = Files.readAllLines(new File(fp).toPath());
        sb.append(lines.stream().collect(Collectors.joining(System.lineSeparator())));
    });
};
executorService.scheduleAtFixedRate(collect, 0, 5, TimeUnit.SECONDS);
StringBuilder sb=新建StringBuilder();
可运行收集=()->{
String[]filepath={”“};//假设它不是空的
String[]fileContent=新字符串[filepath.length];
Arrays.stream(filepath).forEach(fp->{
List line=Files.readAllLines(新文件(fp.toPath());
sb.append(lines.stream().collect(collector.joining(System.lineSeparator())));
});
};
executorService.scheduleAtFixedRate(收集,0,5,时间单位秒);