我正在用 Java 投影一个监控客户端,它将从/proc
. 我有一个主班MonitoringClient
和ProcParser
班。我的ProcParser
扩展Thread
。
我认为这是实例化许多(四个:cpu、内存、磁盘和网络)ProcParser
对象的好方法,传递我想在构造函数中测量的组件之一。然后,在 run 方法中,我在 then 之间切换 - 每个对象都会解析其中一个。代码将类似于:
ProcParser pp_Cpu = new ProcParser(UsageType.CPU, 1000, Pid.getPid());
ProcParser pp_Memory = new ProcParser(UsageType.MEMORY, 1000, Pid.getPid());
ProcParser pp_Disk = new ProcParser(UsageType.DISK, 1000, Pid.getPid());
ProcParser pp_Network = new ProcParser(UsageType.NETWORK, 1000, Pid.getPid());
ArrayList<String> cpuData = null;
ArrayList<String> memoryData = null;
ArrayList<String> diskData = null;
ArrayList<String> networkData = null;
pp_Cpu.start();
pp_Memory.start();
pp_Disk.start();
pp_Network.start();
for (;;) {
if (pp_Cpu.isReady() && pp_Memory.isReady()
&& pp_Disk.isReady() && pp_Network.isReady()) {
cpuData = pp_Cpu.getUsageData();
memoryData = pp_Memory.getUsageData();
diskData = pp_Disk.getUsageData();
networkData = pp_Network.getUsageData();
} else {
sleep(1000); //this does not work. I have to extend to Thread also the main class?
}
}
我如何不时查看收集的值?另外,从 Java 线程的角度来看,我创建线程(每个主要组件一个)的方式是一种好方法吗?提前致谢。
在 ProcParser 中运行方法。
@Override
public void run() {
if ((this.usageType == null) || (this.sleepTime < 0) || (this.processPid < 0)) {
throw new IllegalArgumentException();
}
//Forever sleep and gather the usage values
for (;;) {
this.ready = false;
switch (this.usageType) {
case CPU:
this.gatherCpuUsage();
this.ready = true;
break;
case MEMORY:
this.gatherMemoryUsage(this.processPid);
this.ready = true;
break;
case DISK:
this.gatherDiskUsage();
break;
case NETWORK:
this.gatherNetworkUsage();
break;
case PARTITION:
this.gatherPartitionUsage();
break;
default:
break;
}
}
}