Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:41:23+00:00 2026-06-03T10:41:23+00:00

I’m projecting a monitoring client in Java which will read information from /proc .

  • 0

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;
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:41:29+00:00Added an answer on June 3, 2026 at 10:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.