Java每个对象都有一把锁,当前进程使用对象锁1,没有释放该锁,又想要去获取另一把对象锁2,而对象锁2被另外一个线程持有,没有释放,这就很容易出现死锁

1.死锁实例

public class DeadLockTest {

	private static Object object1 = new Object();
private static Object object2 = new Object(); public static void main(String[] args) { new Thread(new Runnable() { @Override
public void run() {
synchronized (object1) {
System.out.println("线程1获取object1锁...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (object2) {
System.out.println("线程1获取object2锁...");
}
}
}
}).start(); new Thread(new Runnable() { @Override
public void run() {
synchronized (object2) {
System.out.println("线程2获取object2锁...");
synchronized (object1) {
System.out.println("线程2获取object1锁...");
}
}
}
}).start(); System.out.println("main 线程执行完毕...");
}
}

分析:线程1持有object1的锁,同时又想去获取object2的锁,而此时线程2又持有object2的锁没有释放,又想去获取object1的锁,这就造成了死锁。

2.通过命令查看死锁

前提:进入jdk的bin目录下

2.1 jps

jps命令查看Java相关进程

2.2 jstack

jstack:查看分析具体的进程执行情况

运行上面的程序,执行jps命令,发现上面代码的进程号为14888

然后执行jstack -l pid(进程号)

C:\Program Files\Java\jdk1.8.0_51\bin>jstack -l 14144
2018-07-15 23:44:43
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000004dd3800 nid=0x3808 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"Thread-1" #12 prio=5 os_prio=0 tid=0x000000001c1a1000 nid=0x2af4 waiting for monitor entry [0x000000001ce2f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.liu.demo.test.thread.DeadLockTest$2.run(DeadLockTest.java:36)
        - waiting to lock <0x00000000d60ba308> (a java.lang.Object)
        - locked <0x00000000d60ba318> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

Locked ownable synchronizers:
        - None

"Thread-0" #11 prio=5 os_prio=0 tid=0x000000001c1a0000 nid=0x6d0 waiting for monitor entry [0x000000001cd2f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.liu.demo.test.thread.DeadLockTest$1.run(DeadLockTest.java:23)
        - waiting to lock <0x00000000d60ba318> (a java.lang.Object)
        - locked <0x00000000d60ba308> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

Locked ownable synchronizers:
        - None

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001c0df000 nid=0x2c0c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"C1 CompilerThread3" #9 daemon prio=9 os_prio=2 tid=0x000000001c0c7000 nid=0x41ac waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"C2 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000001c0bf800 nid=0x27e8 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x000000001c0be000 nid=0x3728 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000001c0b9800 nid=0x2f08 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c0b6800 nid=0x377c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001c06a800 nid=0x1f48 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
        - None

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000004ecc000 nid=0x798 in Object.wait() [0x000000001bf2f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5f06f58> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x00000000d5f06f58> (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)

Locked ownable synchronizers:
        - None

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000004ec5000 nid=0x43ec in Object.wait() [0x000000001be2f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5f06998> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
        - locked <0x00000000d5f06998> (a java.lang.ref.Reference$Lock)

Locked ownable synchronizers:
        - None

"VM Thread" os_prio=2 tid=0x0000000019f38000 nid=0x2f88 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000004de9800 nid=0x1324 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000004deb000 nid=0x1e24 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000004dec800 nid=0x1bb4 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000004dee000 nid=0x36c4 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000004df0800 nid=0x3838 runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000004df2800 nid=0x1d2c runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000004df5800 nid=0x4214 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000004df7000 nid=0x3018 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001c159000 nid=0x266c waiting on condition

JNI global references: 6

Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x0000000004eca3e8 (object 0x00000000d60ba308, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x0000000004ecb7d8 (object 0x00000000d60ba318, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at com.liu.demo.test.thread.DeadLockTest$2.run(DeadLockTest.java:36)
        - waiting to lock <0x00000000d60ba308> (a java.lang.Object)
        - locked <0x00000000d60ba318> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)
"Thread-0":
        at com.liu.demo.test.thread.DeadLockTest$1.run(DeadLockTest.java:23)
        - waiting to lock <0x00000000d60ba318> (a java.lang.Object)
        - locked <0x00000000d60ba308> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

从上面的结果中可以看到,两个线程都处于BLOCKED阻塞状态,并且明确指出发现了一个死锁;

Java死锁以及命令检测的更多相关文章

  1. 一文学会Java死锁和CPU 100% 问题的排查技巧

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开 工欲善其事,必先利其器 00 本文简介 作为一名搞技术的程序猿或者是攻城狮,想必你应该是对下面这两个问题有所了解,说不定你在 ...

  2. 【性能优化】一文学会Java死锁和CPU100%问题的排查技巧

    原文链接: 00 本文简介 作为一名搞技术的程序猿或者是攻城狮,想必你应该是对下面这两个问题有所了解,说不定你在实际的工作或者面试就有遇到过: 第一个问题:Java死锁如何排查和解决? 第二个问题:服 ...

  3. Java虚拟机监控命令

    熟悉java的人都知道jdk的bin目录中有很多小工具,其中就包括用于监视虚拟机和故障处理的工具,今天就来仔细了解下各个工具的用法 jps JVM Process Status Tool,用于显示指定 ...

  4. Java死锁排查和Java CPU 100% 排查的步骤整理

    ================================================= 人工智能教程.零基础!通俗易懂!风趣幽默!大家可以看看是否对自己有帮助! 点击查看高清无码教程 == ...

  5. java问题排查命令

    java问题排查命令 jps:查看java进程 jmap:导出堆详细信息(与jhat一起使用) jhat:分析Java堆的命令(与jmap一起使用) jstack:可以定位到线程堆栈,根据堆栈信息我们 ...

  6. 如何通过编程发现Java死锁

    本文由 ImportNew - rookie_sam 翻译自 Dzone.欢迎加入翻译小组.转载请见文末要求. 死锁是指,两个或多个动作一直在等待其他动作完成而使得所有动作都始终处在阻塞的状态.想要在 ...

  7. java 执行shell命令遇到的坑

    正常来说java调用shell命令就是用 String[] cmdAry = new String[]{"/bin/bash","-c",cmd} Runtim ...

  8. java与javac命令的功用

    一.javac用来编译java程序,比如说我写了一个Server.java文件,首先通过命令行进入.java文件所在的路径, 然后通过输入 javac Server.java 命令行来完成编译,编译之 ...

  9. 如何解救在异步Java代码中已检测的异常

    Java语言通过已检测异常语法所提供的静态异常检测功能非常实用,通过它程序开发人员可以用很便捷的方式表达复杂的程序流程. 实际上,如果某个函数预期将返回某种类型的数据,通过已检测异常,很容易就可以扩展 ...

随机推荐

  1. [AOP] 7. 一些自定义的Aspect - Circuit Breaker

    Circuit Breaker(断路器)模式 关于断路器模式是在微服务架构/远程调用环境下经常被使用到的一个模式.它的作用一言以蔽之就是提高系统的可用性,在出现的问题通过服务降级的手段来保证系统的整体 ...

  2. 随手一写就是一个侧滑关闭activity

    刚忙完一段时间,今天刚清闲会,就把以前写的东西整理整理.于是冥冥中发现有些东西完全可以共享出来,毕竟那么常见,而且简单实用. 实现原因 其实侧滑关闭activity在网上也有大量的文章去介绍他,我也有 ...

  3. java入门学习(5)—面向对象注意点总结

    1.一个类里面最多有5种成份(属性,方法,构造器,还有两种还没有涉及). 2.定义方法时又返回值的保证最起码有一个有效的return语句,最起码让其在编译的时候就识别到,而不是经过判断识别,如通过if ...

  4. python常用模块之random模块

    python常用模块之random模块 在程序中很多会用到随机字符,比如登陆网站的随机验证码,通过random模块可以很容易生成随机字符串 1.random.randrange():返回1-10之间的 ...

  5. Android 实现两屏幕互相滑动

    Android 实现两屏幕互相滑动 下文来自: http://blog.csdn.net/song_shi_chao/article/details/7081664 ----------------- ...

  6. Sprint Boot 学习Q&A

    [ERROR] Failed to execute goal pl.project13.maven:git-commit-id-plugin:2.1.11:revision (default) on ...

  7. java中的Reference

    这两天又重新学习了一下Reference,根据网上的资源做了汇总. Java中的引用主要有4种: 强引用 StrongReference: Object obj = new Object(); obj ...

  8. Hypergraph Models超图模型

    最近看了篇Paper(Hyperspectral Image Classification Through Bilayer Graph-Based Learning),里面出现了一个超图(Hyperg ...

  9. GCD 深入理解(二)

    转自@nixzhu的GitHub主页(译者:Riven.@nixzhu),原文<Grand Central Dispatch In-Depth: Part 2/2> 欢迎来到GCD深入理解 ...

  10. Windows10安装MySQL8.0

    1.到MySQL官网下载安装包:https://dev.mysql.com/downloads/mysql/:选择8.0版本: 2.将下载好的安装包(mysql-8.0.12-winx64 .zip) ...