Mock problem code

Construct an infinite loop, resulting in 100% CPU usage.

> vim InfiniteLoop.java public class InfiniteLoop { public static void main(String[] args) { Runnable target; Thread thread=new Thread(new Runnable() { @Override public void run() { long i=0; while (true){ i++; }}}); thread.setName("rumenz"); thread.start(); } } > javac InfiniteLoop.javaCopy the code

Running problem code

> java InfiniteLoop
Copy the code

The system CPU is 100%

> top

6076 root      20   0 7096732  18972  10648 S 100.0  0.1   7:42.51 java InfiniteLoop
Copy the code

You get process number 6076

According to the top command, the Java process with PID 6076 occupies 100% of the CPU and is faulty.

Find the specific thread number

> top -Hp 6076

6096 root      20   0 7096732  18972  10648 R 99.7  0.1   9:09.92 java 
Copy the code

You get the thread number 6096

Converts the thread number to hexadecimal

> printf "%x\n" 6096
17d0
Copy the code

If you have everything, start using itjstackPrint stack information

> jstack 6076 | grep 17d0 -A 30 "rumenz" #10 prio=5 os_prio=0 tid=0x00007fe0580f9000 nid=0x17d0 runnable [0x00007fe04431d000] java.lang.Thread.State: RUNNABLE at InfiniteLoop$1.run(InfiniteLoop.java:11) at java.lang.Thread.run(Thread.java:748) "Service Thread" #9 daemon  prio=9 os_prio=0 tid=0x00007fe0580e5800 nid=0x17ce runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007fe0580c8000 nid=0x17cd waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007fe0580c6000 nid=0x17cc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fe0580c4000 nid=0x17cb waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fe0580c1000 nid=0x17ca waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fe0580bf800 nid=0x17c9 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fe05808e800 nid=0x17c8 in Object.wait() [0x00007fe044b25000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076d408ed8> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x000000076d408ed8> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)Copy the code

At InfiniteLoop$1.run(InfiniteLoop. Java :11), the InfiniteLoop$1.run(InfiniteLoop. Java :11), the InfiniteLoop$1.run(InfiniteLoop.

Summary to solve JAVA,CPU 100% problems

  • topTo find theCPU 100%The process of no.pid
  • top -Hp pidFind out the processpidThe most ofCPUNumber of threadstid
  • printf "%x\n" tidtidConvert to hexadecimal16tid
  • jstack pid | grep 16tid -A 30Print stack information
  • Problem handling code

Follow the wechat official account: [entry station], unlock more knowledge points.