示例代码:

public class CPULockTest {

    private static Object lock1 = new Object();
private static Object lock2 = new Object(); public static void main(String[] args) {
new Thread(()->{
synchronized (lock1){
try {
System.out.println(Thread.currentThread().getName()+"得到lock1");
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2){
System.out.println(Thread.currentThread().getName()+"得到lock2");
}
}
},"线程1").start(); new Thread(()->{
synchronized (lock2){
try {
System.out.println(Thread.currentThread().getName()+"得到lock2");
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1){
System.out.println(Thread.currentThread().getName()+"得到lock1");
}
}
},"线程2").start();
}
}

找出pid(进程ID)

top命令

在linux环境下,可以通过top命令查看各个进程的cpu使用情况,默认按cpu使用率排序

jps命令

显示指定系统内所有的HotSpot虚拟机进程。

通过进程id看线程情况

linux:通过top -Hp 4548可以查看该进程下各个线程的cpu使用情况,有线程的pid;

mac:

jstack查看当前进程的状态

➜  ~ jstack 4548
2017-03-14 09:57:31
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.102-b14 mixed mode):
"Attach Listener" #13 daemon prio=9 os_prio=31 tid=0x00007f8c1a222000 nid=0x350f waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"DestroyJavaVM" #12 prio=5 os_prio=31 tid=0x00007f8c1a26c800 nid=0x1703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"线程2" #11 prio=5 os_prio=31 tid=0x00007f8c1a223000 nid=0x5103 waiting for monitor entry [0x000070000134f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at cn.gov.zcy.fixed.CPULockTest.lambda$main$1(CPULockTest.java:35)
- waiting to lock <0x0000000795b08d58> (a java.lang.Object)
- locked <0x0000000795b08d68> (a java.lang.Object)
at cn.gov.zcy.fixed.CPULockTest$$Lambda$2/859417998.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"线程1" #10 prio=5 os_prio=31 tid=0x00007f8c19967800 nid=0x4f03 waiting for monitor entry [0x000070000124c000]
java.lang.Thread.State: BLOCKED (on object monitor)
at cn.gov.zcy.fixed.CPULockTest.lambda$main$0(CPULockTest.java:21)
- waiting to lock <0x0000000795b08d68> (a java.lang.Object)
- locked <0x0000000795b08d58> (a java.lang.Object)
at cn.gov.zcy.fixed.CPULockTest$$Lambda$1/1020391880.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Monitor Ctrl-Break" #9 daemon prio=5 os_prio=31 tid=0x00007f8c1a224800 nid=0x4d03 runnable [0x0000700001149000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
at java.lang.Thread.run(Thread.java:745) "Service Thread" #8 daemon prio=9 os_prio=31 tid=0x00007f8c1a029800 nid=0x4903 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007f8c1a83f800 nid=0x4703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007f8c19845000 nid=0x4503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007f8c1a800800 nid=0x4303 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007f8c1a028000 nid=0x320f runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007f8c1a82d000 nid=0x3003 in Object.wait() [0x000070000092e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795588e98> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x0000000795588e98> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) "Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007f8c19805000 nid=0x2e03 in Object.wait() [0x000070000082b000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795586b40> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000795586b40> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=31 tid=0x00007f8c1a014000 nid=0x2c03 runnable
"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007f8c1a818800 nid=0x2403 runnable
"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007f8c1a000800 nid=0x2603 runnable
"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007f8c19809000 nid=0x2803 runnable
"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007f8c19809800 nid=0x2a03 runnable
"VM Periodic Task Thread" os_prio=31 tid=0x00007f8c1b029800 nid=0x4b03 waiting on condition
JNI global references: 324 Found one Java-level deadlock:
=============================
"线程2":
waiting to lock monitor 0x00007f8c1a0176b8 (object 0x0000000795b08d58, a java.lang.Object),
which is held by "线程1"
"线程1":
waiting to lock monitor 0x00007f8c1a0178c8 (object 0x0000000795b08d68, a java.lang.Object),
which is held by "线程2" Java stack information for the threads listed above:
===================================================
"线程2":
at cn.gov.zcy.fixed.CPULockTest.lambda$main$1(CPULockTest.java:35)
- waiting to lock <0x0000000795b08d58> (a java.lang.Object)
- locked <0x0000000795b08d68> (a java.lang.Object)
at cn.gov.zcy.fixed.CPULockTest$$Lambda$2/859417998.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"线程1":
at cn.gov.zcy.fixed.CPULockTest.lambda$main$0(CPULockTest.java:21)
- waiting to lock <0x0000000795b08d68> (a java.lang.Object)
- locked <0x0000000795b08d58> (a java.lang.Object)
at cn.gov.zcy.fixed.CPULockTest$$Lambda$1/1020391880.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745) Found 1 deadlock.

jstack命令生成的thread dump信息包含了JVM中所有存活的线程,为了分析指定线程,必须找出对应线程的调用栈,应该如何找?

在top命令中,已经获取到了占用cpu资源较高的线程pid,将该pid转成16进制的值,在thread dump中每个线程都有一个nid,找到对应的nid即可;隔段时间再执行一次stack命令获取thread dump,区分两份dump是否有差别。

从cpu负载到jstack分析线程状态的更多相关文章

  1. 转: 如何使用jstack分析线程状态

    这个讲的好系列:  如何使用jstack分析线程状态 转:http://www.jianshu.com/p/6690f7e92f27 背景 记得前段时间,同事说他们测试环境的服务器cpu使用率一直处于 ...

  2. 如何使用jstack分析线程状态

    背景 记得前段时间,同事说他们测试环境的服务器cpu使用率一直处于100%,本地又没有什么接口调用,为什么会这样?cpu使用率居高不下,自然是有某些线程一直占用着cpu资源,那又如何查看占用cpu较高 ...

  3. jstack分析线程死锁

    一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  4. jstack 查看线程状态

    使用jstack pid命令可以查看JVM的线程状态,其中值得关注的线程状态有:死锁,Deadlock(重点关注)执行中,Runnable等待资源,Waiting on condition(重点关注) ...

  5. 学习笔记: jstack与线程状态

    jstatck可以打印JVM内部所有线程 1.查看有哪些java进程 2.查看所有线程的信息 重定向到5579.txt文件中 jstack 5579 > 5579.txt 3.线程的状态 New ...

  6. cpu负载过高分析

    如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代码导致CPU过载? 步骤一.找到最耗CPU的进程 工具:top 方法: 执行top -c ,显示进程运行信息列表 键入P (大写p) ...

  7. java命令分析线程死锁以及内存泄漏

    一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  8. 面试官:都说阻塞 I/O 模型将会使线程休眠,为什么 Java 线程状态却是 RUNNABLE?

    摘要: 原创出处 https://studyidea.cn 「公众号:程序通事 」欢迎关注和转载,保留摘要,谢谢! 使用 Java 阻塞 I/O 模型读取数据,将会导致线程阻塞,线程将会进入休眠,从而 ...

  9. Java内存泄漏分析系列之四:jstack生成的Thread Dump日志线程状态

    原文地址:http://www.javatang.com Thread Dump日志的线程信息 以下面的日志为例: "resin-22129" daemon prio=10 tid ...

随机推荐

  1. knowledge_map 修改笔记

    20150110 knowledge_map修改的地方: 1.dialog的调整.把结构图中的dialog(另存,新建知识点)调整到全局中(我的列表,knowledge_map_js_showKnow ...

  2. Delphi 中 FindWindow 和 FindWindowEx 的语法和用法

    FindWindow(lpClassName,        {窗口的类名}lpWindowName: PChar {窗口的标题}): HWND;              {返回窗口的句柄; 失败返 ...

  3. SQL笔记---分页

    随用随想,随用随记. 通过实际应用掌握SQL语句. 一. SQL分页 1. 第一种方法:利用ID大于多少进行筛选 SELECT TOP 20        *FROM    dbo.WMS_Stock ...

  4. SQL语句小tips(持续更新)

    统计非法数据 判断people_id是否是32为字母组成的,统计不满足要求的数据 SELECT COUNT(IF(BINARY people_id NOT REGEXP '^[0-9a-z]{32}' ...

  5. 记录一下将SqlLocalDb数据迁移到Sql Server Express2017的过程!

    当初为了开发方便,使用了SqlLocalDb,然后再测试期产生了很多有用的数据,客户说不能删除了.麻烦 先将数据库文件下载回来,然后安装SSMS,在连接数据库地址那里,填写“(LocalDB)\MSS ...

  6. Unreal Open Day游记

    前几天去参加了Unreal Open Day,周四早上从北京出发,坐地铁跟徐导,呵呵,simon他们汇合后,打车去了北京南站.一路上有小雨,不禁让人多少有点担心堵车,好在一路顺利.由于还没有一台较牛的 ...

  7. 将tomcat添加到服务中

    转载自:http://blog.csdn.net/zjd934784273/article/details/47701011(windows) :http://www.cnblogs.com/ilah ...

  8. elk部署心得

    一.ElasticSearch 部署 1.配置文件里node.name 要不一致. vim /etc/elasticsearch cluster.name: aubin-cluster # 集群名称 ...

  9. Java Web相关技术(汇聚页)

    Java Web相关技术(汇聚页) 初学Java Web(2)——搭建Java Web开发环境

  10. Binary Search-483. Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...