linux 查看java线程与linux线程关系

linux 查看占用cpu高的 java 线程

linux 排查cpu占用100%故障

##java程序

import java.util.Scanner;
public class JavaThreadIDName {

    public static void main(String[] args) {
        Thread t = Thread.currentThread();
        t.setName("laoyoutiao");
        System.out.println("Thread ID: " + t.getId());
        System.out.println("Thread Name: " + t.getName());
        Scanner scanner = new Scanner(System.in);
        String inputString = scanner.nextLine(); // 等待用户输入一行文本
        System.out.println("你输入的是: " + inputString);
    }

}

##linux查看java进程

jps
11409 Jps
10133 JavaThreadIDName

##linux查看java进程最耗cpu资源java线程

top -H -bn 1 -p 10133
top - 18:54:21 up  1:21,  1 user,  load average: 0.09, 0.38, 0.26
Threads:  17 total,   0 running,  17 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.0 us,  0.3 sy,  0.1 ni, 98.3 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16424100 total,  9156748 free,  3143220 used,  4124132 buff/cache
KiB Swap:   998396 total,   998396 free,        0 used. 12839668 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
10133 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 java
10134 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:01.06 laoyoutiao
10135 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 GC Thread#0
10136 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 G1 Main Marker
10137 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 G1 Conc#0
10138 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 G1 Refine#0
10139 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.02 G1 Young RemSet
10140 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.01 VM Thread
10141 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 Reference Handl
10142 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 Finalizer
10143 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 Signal Dispatch
10144 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 Service Thread
10145 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.37 C2 CompilerThre
10146 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.76 C1 CompilerThre
10147 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 Sweeper thread
10148 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.00 Common-Cleaner
10151 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:00.10 VM Periodic Tas

##linux查看java线程

ps -T -p  10133
  PID  SPID TTY          TIME CMD
10133 10133 pts/19   00:00:00 java
10133 10134 pts/19   00:00:01 laoyoutiao
10133 10135 pts/19   00:00:00 GC Thread#0
10133 10136 pts/19   00:00:00 G1 Main Marker
10133 10137 pts/19   00:00:00 G1 Conc#0
10133 10138 pts/19   00:00:00 G1 Refine#0
10133 10139 pts/19   00:00:02 G1 Young RemSet
10133 10140 pts/19   00:00:01 VM Thread
10133 10141 pts/19   00:00:00 Reference Handl
10133 10142 pts/19   00:00:00 Finalizer
10133 10143 pts/19   00:00:00 Signal Dispatch
10133 10144 pts/19   00:00:00 Service Thread
10133 10145 pts/19   00:00:00 C2 CompilerThre
10133 10146 pts/19   00:00:00 C1 CompilerThre
10133 10147 pts/19   00:00:00 Sweeper thread
10133 10148 pts/19   00:00:00 Common-Cleaner
10133 10151 pts/19   00:00:09 VM Periodic Tas

##linux查看java堆栈

jstack 10133
2024-05-16 09:31:15
Full thread dump OpenJDK 64-Bit Server VM (11.0.21-internal+0-adhoc.yym.jdk11u-master mixed mode):

Threads class SMR info:
_java_thread_list=0x00007f83c40021f0, length=10, elements={
0x00007f8408031800, 0x00007f8408312000, 0x00007f8408315000, 0x00007f8408350000,
0x00007f8408352800, 0x00007f8408355000, 0x00007f8408357800, 0x00007f840835a000,
0x00007f84083af800, 0x00007f83c4001000
}
_java_thread_list_alloc_cnt=15, _java_thread_list_free_cnt=14, _java_thread_list_max=11, _nested_thread_list_max=0
_tlh_cnt=55480, _tlh_times=3, avg_tlh_time=0.00, _tlh_time_max=1
_deleted_thread_cnt=2, _deleted_thread_times=0, avg_deleted_thread_time=0.00, _deleted_thread_time_max=0
_delete_lock_wait_cnt=0, _delete_lock_wait_max=0
_to_delete_list_cnt=0, _to_delete_list_max=1

"laoyoutiao" #1 prio=5 os_prio=0 cpu=1062.77ms elapsed=52744.36s tid=0x00007f8408031800 nid=0x2796 runnable  [0x00007f8411459000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_in_native
Thread: 0x00007f8408031800  [0x2796] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_in_native
	at java.io.FileInputStream.readBytes(java.base/Native Method)
	at java.io.FileInputStream.read(java.base/FileInputStream.java:279)
	at java.io.BufferedInputStream.read1(java.base/BufferedInputStream.java:290)
	at java.io.BufferedInputStream.read(java.base/BufferedInputStream.java:351)
	- locked <0x000000071510dfa8> (a java.io.BufferedInputStream)
	at sun.nio.cs.StreamDecoder.readBytes(java.base/StreamDecoder.java:284)
	at sun.nio.cs.StreamDecoder.implRead(java.base/StreamDecoder.java:326)
	at sun.nio.cs.StreamDecoder.read(java.base/StreamDecoder.java:178)
	- locked <0x0000000714916f58> (a java.io.InputStreamReader)
	at java.io.InputStreamReader.read(java.base/InputStreamReader.java:181)
	at java.io.Reader.read(java.base/Reader.java:189)
	at java.util.Scanner.readInput(java.base/Scanner.java:882)
	at java.util.Scanner.findWithinHorizon(java.base/Scanner.java:1796)
	at java.util.Scanner.nextLine(java.base/Scanner.java:1649)
	at JavaThreadIDName.main(JavaThreadIDName.java:10)

"Reference Handler" #2 daemon prio=10 os_prio=0 cpu=0.22ms elapsed=52744.24s tid=0x00007f8408312000 nid=0x279d waiting on condition  [0x00007f83e844a000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f8408312000  [0x279d] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked
	at java.lang.ref.Reference.waitForReferencePendingList(java.base/Native Method)
	at java.lang.ref.Reference.processPendingReferences(java.base/Reference.java:241)
	at java.lang.ref.Reference$ReferenceHandler.run(java.base/Reference.java:213)

"Finalizer" #3 daemon prio=8 os_prio=0 cpu=1.21ms elapsed=52744.24s tid=0x00007f8408315000 nid=0x279e in Object.wait()  [0x00007f83e8349000]
   java.lang.Thread.State: WAITING (on object monitor)
   JavaThread state: _thread_blocked
Thread: 0x00007f8408315000  [0x279e] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked
	at java.lang.Object.wait(java.base/Native Method)
	- waiting on <0x0000000715109008> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base/ReferenceQueue.java:155)
	- waiting to re-lock in wait() <0x0000000715109008> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base/ReferenceQueue.java:176)
	at java.lang.ref.Finalizer$FinalizerThread.run(java.base/Finalizer.java:170)

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 cpu=0.54ms elapsed=52744.17s tid=0x00007f8408350000 nid=0x279f runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f8408350000  [0x279f] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked

"Service Thread" #5 daemon prio=9 os_prio=0 cpu=0.05ms elapsed=52744.17s tid=0x00007f8408352800 nid=0x27a0 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f8408352800  [0x27a0] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked

"C2 CompilerThread0" #6 daemon prio=9 os_prio=0 cpu=456.01ms elapsed=52744.17s tid=0x00007f8408355000 nid=0x27a1 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f8408355000  [0x27a1] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked
   No compile task

"C1 CompilerThread0" #9 daemon prio=9 os_prio=0 cpu=865.46ms elapsed=52744.17s tid=0x00007f8408357800 nid=0x27a2 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f8408357800  [0x27a2] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked
   No compile task

"Sweeper thread" #10 daemon prio=9 os_prio=0 cpu=0.04ms elapsed=52744.17s tid=0x00007f840835a000 nid=0x27a3 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f840835a000  [0x27a3] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked

"Common-Cleaner" #11 daemon prio=8 os_prio=0 cpu=24.27ms elapsed=52744.12s tid=0x00007f84083af800 nid=0x27a4 in Object.wait()  [0x00007f83cb3fa000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
   JavaThread state: _thread_blocked
Thread: 0x00007f84083af800  [0x27a4] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked
	at java.lang.Object.wait(java.base/Native Method)
	- waiting on <0x0000000715128de8> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base/ReferenceQueue.java:155)
	- waiting to re-lock in wait() <0x0000000715128de8> (a java.lang.ref.ReferenceQueue$Lock)
	at jdk.internal.ref.CleanerImpl.run(java.base/CleanerImpl.java:148)
	at java.lang.Thread.run(java.base/Thread.java:829)
	at jdk.internal.misc.InnocuousThread.run(java.base/InnocuousThread.java:161)

"Attach Listener" #12 daemon prio=9 os_prio=0 cpu=0.15ms elapsed=0.21s tid=0x00007f83c4001000 nid=0x2cca waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   JavaThread state: _thread_blocked
Thread: 0x00007f83c4001000  [0x2cca] State: _at_safepoint _has_called_back 0 _at_poll_safepoint 0
   JavaThread state: _thread_blocked

"VM Thread" os_prio=0 cpu=1264.74ms elapsed=52744.28s tid=0x00007f84082fb800 nid=0x279c runnable  

"GC Thread#0" os_prio=0 cpu=569.98ms elapsed=52744.36s tid=0x00007f8408051000 nid=0x2797 runnable  

"G1 Main Marker" os_prio=0 cpu=4.53ms elapsed=52744.35s tid=0x00007f84080b1800 nid=0x2798 runnable  

"G1 Conc#0" os_prio=0 cpu=0.03ms elapsed=52744.35s tid=0x00007f84080b3800 nid=0x2799 runnable  

"G1 Refine#0" os_prio=0 cpu=4.80ms elapsed=52744.35s tid=0x00007f84081d9800 nid=0x279a runnable  

"G1 Young RemSet Sampling" os_prio=0 cpu=2482.68ms elapsed=52744.35s tid=0x00007f84081db800 nid=0x279b runnable  
"VM Periodic Task Thread" os_prio=0 cpu=10019.83ms elapsed=52743.55s tid=0x00007f84086ba000 nid=0x27a7 waiting on condition  

JNI global refs: 9, weak refs: 0

##

ps显示pid和java nid 计算。可以使用计算器转换16进制

10134 yym       20   0 6895436  98148  28224 S  0.0  0.6   0:01.06 laoyoutiao

"laoyoutiao" #1 prio=5 os_prio=0 cpu=1062.77ms elapsed=52744.36s tid=0x00007f8408031800 nid=0x2796 runnable  [0x00007f8411459000]

由此就可以查看哪个java线程最耗cpu资源。从而解决cpu 100%问题。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值