How to see the percentage of cpu usage of each thread of a java process

Well, I have a cpu usage problem, and I don't know exactly which class or Thread it is consuming ( there is no way to know externally from the code ), I wanted to know if there is any method that shows all active threads with their respective CPU usage. I tried to use a code to see the active threads, but not being able to see the % of the CPU, how to do this?

      ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

            for(Long threadID : threadMXBean.getAllThreadIds()) {
                ThreadInfo info = threadMXBean.getThreadInfo(threadID);
                System.out.println("Nome da thread: " + info.getThreadName());
                System.out.println("Estado da thread: " + info.getThreadState());
                long cpusage = threadMXBean.getThreadCpuTime(threadID);
                System.out.println(String.format("Tempo usando a cpu: %.2f s",cpusage==0?0d:cpusage/100000000d));
            }

I'm running the program in docker on AWS, I don't have access to the terminal, I need to know the information through code, (or some other means of seeing this information)

Author: chrono, 2019-10-08

1 answers

You can use this to run Linux commands. You'll probably be able to get the information with some linux command

Process p = Runtime.getRuntime().exec("ls -aF");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null)
    System.out.println("line: " + s);
p.waitFor();
System.out.println ("exit: " + p.exitValue());
p.destroy();

See Here: https://stackoverflow.com/questions/3403226/how-to-run-linux-commands-in-java

 -1
Author: João Zarate, 2019-10-09 22:07:32