I’m projecting a monitoring client in Java which will read information from /proc. I have a main class MonitoringClient and ProcParser class. My ProcParser extends Thread.
I thought it would be a good approach to instantiate many (four: cpu, memory, disk and network) ProcParser objects, passing one of the components which I want to measure in the constructor. Then, in the run method I switch between then – and each object will parse one of those. A code would be something like:
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?
}
}
How do I peek the gathered values time to time? Also, is the way I’m creating the threads (one for each main component) a good approach from the Java Threading point of view? Thanks in advance.
Run method inside the 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;
}
}
}
I am assuming that your
ProcParserthreads run in the background, read in the CPU data (for example) from/proc, and then sleep. You might want to consider using anAtomicReference. Something like the following:You might want to
set()an unmodifiable list if you are worried about the consumers changing the lists.If this was not an exercise, I’d say that you didn’t need the thread complexity — that it doesn’t buy you much. Choosing which tasks need a thread, how many threads to create, etc.. are critical decisions that need to be made in these cases.
Generically, you should consider making
ProcParserimplementRunnableinstead of extendingThreadand then saynew Thread(new ProcParser(...)). That’s a recommended pattern. You should also always consider using theExecutorServiceclasses returned by theExecutorsclass. Those thread-pools take over the administration of the threads and make your code much simpler.