How can I peek at values from a thread for time to time?
-
06-06-2021 - |
Question
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;
}
}
}
Solution
How do I peek the gathered values time to time?
I am assuming that your ProcParser
threads run in the background, read in the CPU data (for example) from /proc
, and then sleep. You might want to consider using an AtomicReference
. Something like the following:
private final AtomicReference<List<String>> cpuDataReference =
new AtomicReference<List<String>>();
...
ProcParser pp_Cpu =
new ProcParser(UsageType.CPU, 1000, Pid.getPid(), cpuDataReference);
...
// in your `ProcParser`
List<String> cpuDataList = new ArrayList<String>();
// read in the /proc files into the list
...
// save it in the AtomicReference passed into the constructor
dataReference.set(cpuDataList);
// other threads could then get the latest list
List<String> cpuDataList = cpuDataReference.get();
You might want to set()
an unmodifiable list if you are worried about the consumers changing the lists.
Is the way I'm creating the threads (one for each main component) a good approach from the Java Threading point of view?
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 ProcParser
implement Runnable
instead of extending Thread
and then say new Thread(new ProcParser(...))
. That's a recommended pattern. You should also always consider using the ExecutorService
classes returned by the Executors
class. Those thread-pools take over the administration of the threads and make your code much simpler.
OTHER TIPS
Instead of polling is_ready
, look into Object.wait
and Object.notifyAll
. You can write a ProcParser.waitUntilReady
method using Object.wait
and use Object.notifyAll
to wake up any threads that are waiting until its ready when the working thread produces a result.
Alternatively, since what your parser is doing is producing a list of items, look into producer consumer queues in java.util.concurrent
.