我一直在研究为特定过程实现以下数据的最佳方法:
我决定使用 OSHI(操作系统和硬件信息)API。对我来说不幸的是,这个 API 没有给我开箱即用的信息,它需要一些关于如何计算的基本知识,例如每个进程的 CPU 使用率。
我的问题是:如何通过进程 id 获取内存、磁盘、网络使用情况?
使用以下每个进程的 CPU 使用数据示例
例如:
要获取 claculator.exe 运行进程的实际 CPU 使用率:
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
public class processCPUusage {
public static void main(String[] args) throws InterruptedException {
OSProcess process;
long currentTime,previousTime = 0,timeDifference;
double cpu;
int pid = 7132;
SystemInfo si = new SystemInfo();
OperatingSystem os = si.getOperatingSystem();
CentralProcessor processor = si.getHardware().getProcessor();
int cpuNumber = processor.getLogicalProcessorCount();
boolean processExists = true;
while (processExists) {
process …Run Code Online (Sandbox Code Playgroud) Java 8.
至少对我来说,将forEach循环转换为lambda表达式是可以理解的.另一方面,转换基于条件的for循环不是.
如果可能,我的问题是:如何将以下for循环转换为lambda表达式
List<Field> fields = new LinkedList<>();
for (Class<?> c = this.getClass(); c != null; c = c.getSuperclass())
Collections.addAll(fields, c.getDeclaredFields());
Run Code Online (Sandbox Code Playgroud)
提前谢谢了,
〜本.