死锁

时间:2019-07-11
本文章向大家介绍死锁,主要包括死锁使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
public class DeadThread {

    private static Object monitorA = new Object();

    private static Object monitorB = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run()   {
                synchronized(monitorA) {
                    System.out.println("线程A获取已获取锁A");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("线程A准备获取锁B");
                    synchronized(monitorB) {
                        System.out.println("线程A已经获取锁B");
                    }
                }
                System.out.println("线程A run方法结束");
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run()   {
                synchronized(monitorB) {
                    System.out.println("线程B获取已获取锁B");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("线程B准备获取锁A");
                    synchronized(monitorA) {
                        System.out.println("线程B已经获取锁A");
                    }
                }
                System.out.println("线程B run方法结束");
            }
        });

        threadA.start();
        threadB.start();
    }
}

运行结果

D:\java\jdk1.8.0_191\bin\java.exe -javaagent:D:\program\ideaIU-2018.3.4.win\lib\idea_rt.jar=51732:D:\program\ideaIU-2018.3.4.win\bin -Dfile.encoding=UTF-8 -classpath D:\java\jdk1.8.0_191\jre\lib\charsets.jar;D:\java\jdk1.8.0_191\jre\lib\deploy.jar;D:\java\jdk1.8.0_191\jre\lib\ext\access-bridge-64.jar;D:\java\jdk1.8.0_191\jre\lib\ext\cldrdata.jar;D:\java\jdk1.8.0_191\jre\lib\ext\dnsns.jar;D:\java\jdk1.8.0_191\jre\lib\ext\jaccess.jar;D:\java\jdk1.8.0_191\jre\lib\ext\jfxrt.jar;D:\java\jdk1.8.0_191\jre\lib\ext\localedata.jar;D:\java\jdk1.8.0_191\jre\lib\ext\nashorn.jar;D:\java\jdk1.8.0_191\jre\lib\ext\sunec.jar;D:\java\jdk1.8.0_191\jre\lib\ext\sunjce_provider.jar;D:\java\jdk1.8.0_191\jre\lib\ext\sunmscapi.jar;D:\java\jdk1.8.0_191\jre\lib\ext\sunpkcs11.jar;D:\java\jdk1.8.0_191\jre\lib\ext\zipfs.jar;D:\java\jdk1.8.0_191\jre\lib\javaws.jar;D:\java\jdk1.8.0_191\jre\lib\jce.jar;D:\java\jdk1.8.0_191\jre\lib\jfr.jar;D:\java\jdk1.8.0_191\jre\lib\jfxswt.jar;D:\java\jdk1.8.0_191\jre\lib\jsse.jar;D:\java\jdk1.8.0_191\jre\lib\management-agent.jar;D:\java\jdk1.8.0_191\jre\lib\plugin.jar;D:\java\jdk1.8.0_191\jre\lib\resources.jar;D:\java\jdk1.8.0_191\jre\lib\rt.jar;D:\java\IdeaProjects\message\target\classes;D:\java\jar\maven\org\springframework\cloud\spring-cloud-netflix-eureka-server\2.0.0.RELEASE\spring-cloud-netflix-eureka-server-2.0.0.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-web\2.0.4.RELEASE\spring-boot-starter-web-2.0.4.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-json\2.0.4.RELEASE\spring-boot-starter-json-2.0.4.RELEASE.jar;D:\java\jar\maven\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;D:\java\jar\maven\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;D:\java\jar\maven\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-tomcat\2.0.4.RELEASE\spring-boot-starter-tomcat-2.0.4.RELEASE.jar;D:\java\jar\maven\org\apache\tomcat\embed\tomcat-embed-core\8.5.32\tomcat-embed-core-8.5.32.jar;D:\java\jar\maven\org\apache\tomcat\embed\tomcat-embed-el\8.5.32\tomcat-embed-el-8.5.32.jar;D:\java\jar\maven\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.32\tomcat-embed-websocket-8.5.32.jar;D:\java\jar\maven\org\hibernate\validator\hibernate-validator\6.0.11.Final\hibernate-validator-6.0.11.Final.jar;D:\java\jar\maven\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\java\jar\maven\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\java\jar\maven\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\java\jar\maven\org\springframework\spring-web\5.0.8.RELEASE\spring-web-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\spring-beans\5.0.8.RELEASE\spring-beans-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\spring-webmvc\5.0.8.RELEASE\spring-webmvc-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\spring-aop\5.0.8.RELEASE\spring-aop-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\spring-context\5.0.8.RELEASE\spring-context-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\spring-expression\5.0.8.RELEASE\spring-expression-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-actuator\2.0.4.RELEASE\spring-boot-starter-actuator-2.0.4.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-actuator-autoconfigure\2.0.4.RELEASE\spring-boot-actuator-autoconfigure-2.0.4.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-actuator\2.0.4.RELEASE\spring-boot-actuator-2.0.4.RELEASE.jar;D:\java\jar\maven\io\micrometer\micrometer-core\1.0.6\micrometer-core-1.0.6.jar;D:\java\jar\maven\org\hdrhistogram\HdrHistogram\2.1.10\HdrHistogram-2.1.10.jar;D:\java\jar\maven\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-freemarker\2.0.4.RELEASE\spring-boot-starter-freemarker-2.0.4.RELEASE.jar;D:\java\jar\maven\org\freemarker\freemarker\2.3.28\freemarker-2.3.28.jar;D:\java\jar\maven\org\springframework\spring-context-support\5.0.8.RELEASE\spring-context-support-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\cloud\spring-cloud-commons\2.0.0.RELEASE\spring-cloud-commons-2.0.0.RELEASE.jar;D:\java\jar\maven\org\springframework\security\spring-security-crypto\5.0.7.RELEASE\spring-security-crypto-5.0.7.RELEASE.jar;D:\java\jar\maven\org\springframework\cloud\spring-cloud-netflix-core\2.0.0.RELEASE\spring-cloud-netflix-core-2.0.0.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-autoconfigure\2.0.4.RELEASE\spring-boot-autoconfigure-2.0.4.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-aop\2.0.4.RELEASE\spring-boot-starter-aop-2.0.4.RELEASE.jar;D:\java\jar\maven\org\aspectj\aspectjweaver\1.8.13\aspectjweaver-1.8.13.jar;D:\java\jar\maven\org\springframework\cloud\spring-cloud-netflix-eureka-client\2.0.0.RELEASE\spring-cloud-netflix-eureka-client-2.0.0.RELEASE.jar;D:\java\jar\maven\com\netflix\eureka\eureka-client\1.9.2\eureka-client-1.9.2.jar;D:\java\jar\maven\org\codehaus\jettison\jettison\1.3.7\jettison-1.3.7.jar;D:\java\jar\maven\stax\stax-api\1.0.1\stax-api-1.0.1.jar;D:\java\jar\maven\com\netflix\netflix-commons\netflix-eventbus\0.3.0\netflix-eventbus-0.3.0.jar;D:\java\jar\maven\com\netflix\netflix-commons\netflix-infix\0.3.0\netflix-infix-0.3.0.jar;D:\java\jar\maven\commons-jxpath\commons-jxpath\1.3\commons-jxpath-1.3.jar;D:\java\jar\maven\joda-time\joda-time\2.9.9\joda-time-2.9.9.jar;D:\java\jar\maven\org\antlr\antlr-runtime\3.4\antlr-runtime-3.4.jar;D:\java\jar\maven\org\antlr\stringtemplate\3.2.1\stringtemplate-3.2.1.jar;D:\java\jar\maven\antlr\antlr\2.7.7\antlr-2.7.7.jar;D:\java\jar\maven\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;D:\java\jar\maven\org\apache\commons\commons-math\2.2\commons-math-2.2.jar;D:\java\jar\maven\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\java\jar\maven\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\java\jar\maven\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\java\jar\maven\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\java\jar\maven\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\java\jar\maven\org\apache\httpcomponents\httpclient\4.5.6\httpclient-4.5.6.jar;D:\java\jar\maven\org\apache\httpcomponents\httpcore\4.4.10\httpcore-4.4.10.jar;D:\java\jar\maven\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\java\jar\maven\com\google\inject\guice\4.1.0\guice-4.1.0.jar;D:\java\jar\maven\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\java\jar\maven\com\github\vlsi\compactmap\compactmap\1.2.1\compactmap-1.2.1.jar;D:\java\jar\maven\com\github\andrewoma\dexx\dexx-collections\0.2\dexx-collections-0.2.jar;D:\java\jar\maven\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\java\jar\maven\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;D:\java\jar\maven\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;D:\java\jar\maven\com\sun\jersey\jersey-servlet\1.19.1\jersey-servlet-1.19.1.jar;D:\java\jar\maven\com\sun\jersey\jersey-server\1.19.1\jersey-server-1.19.1.jar;D:\java\jar\maven\com\netflix\eureka\eureka-core\1.9.2\eureka-core-1.9.2.jar;D:\java\jar\maven\org\codehaus\woodstox\woodstox-core-asl\4.4.1\woodstox-core-asl-4.4.1.jar;D:\java\jar\maven\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar;D:\java\jar\maven\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\java\jar\maven\com\google\code\findbugs\jsr305\3.0.1\jsr305-3.0.1.jar;D:\java\jar\maven\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\java\jar\maven\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\java\jar\maven\com\google\guava\guava\16.0\guava-16.0.jar;D:\java\jar\maven\javax\inject\javax.inject\1\javax.inject-1.jar;D:\java\jar\maven\com\fasterxml\jackson\dataformat\jackson-dataformat-xml\2.9.6\jackson-dataformat-xml-2.9.6.jar;D:\java\jar\maven\com\fasterxml\jackson\module\jackson-module-jaxb-annotations\2.9.6\jackson-module-jaxb-annotations-2.9.6.jar;D:\java\jar\maven\org\codehaus\woodstox\stax2-api\3.1.4\stax2-api-3.1.4.jar;D:\java\jar\maven\com\fasterxml\woodstox\woodstox-core\5.0.3\woodstox-core-5.0.3.jar;D:\java\jar\maven\com\thoughtworks\xstream\xstream\1.4.10\xstream-1.4.10.jar;D:\java\jar\maven\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;D:\java\jar\maven\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter\2.0.4.RELEASE\spring-boot-starter-2.0.4.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot\2.0.4.RELEASE\spring-boot-2.0.4.RELEASE.jar;D:\java\jar\maven\org\springframework\boot\spring-boot-starter-logging\2.0.4.RELEASE\spring-boot-starter-logging-2.0.4.RELEASE.jar;D:\java\jar\maven\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\java\jar\maven\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\java\jar\maven\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;D:\java\jar\maven\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;D:\java\jar\maven\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\java\jar\maven\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\java\jar\maven\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;D:\java\jar\maven\org\springframework\spring-core\5.0.8.RELEASE\spring-core-5.0.8.RELEASE.jar;D:\java\jar\maven\org\springframework\spring-jcl\5.0.8.RELEASE\spring-jcl-5.0.8.RELEASE.jar;D:\java\jar\maven\org\projectlombok\lombok\1.16.22\lombok-1.16.22.jar;D:\java\jar\maven\org\freeswitch\esl\client\org.freeswitch.esl.client\0.9.2\org.freeswitch.esl.client-0.9.2.jar;D:\java\jar\maven\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\java\jar\maven\org\jboss\netty\netty\3.2.1.Final\netty-3.2.1.Final.jar;D:\java\jar\maven\log4j\log4j\1.2.17\log4j-1.2.17.jar com.matt.message.thread.DeadThread
线程A获取已获取锁A
线程B获取已获取锁B
线程A准备获取锁B
线程B准备获取锁A

 

 


C:\Users\Administrator>jstack 4736
2019-07-11 22:43:59
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.191-b12 mixed mode):

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

"Thread-1" #12 prio=5 os_prio=0 tid=0x0000000059000000 nid=0x17f8 waiting for monitor entry [0x000000005a0af000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.matt.message.thread.DeadThread$2.run(DeadThread.java:45)
- waiting to lock <0x00000000d6451490> (a java.lang.Object)
- locked <0x00000000d64514a0> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:748)

"Thread-0" #11 prio=5 os_prio=0 tid=0x0000000058fff000 nid=0x1298 waiting for monitor entry [0x0000000059faf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.matt.message.thread.DeadThread$1.run(DeadThread.java:25)
- waiting to lock <0x00000000d64514a0> (a java.lang.Object)
- locked <0x00000000d6451490> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:748)

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

"C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x00000000589cf800 nid=0x1014 waiting on condition [0x00000000000000
00]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x00000000589cd800 nid=0x80c waiting on condition [0x000000000000000
0]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x00000000589cc800 nid=0x1398 waiting on condition [0x00000000000000
00]
java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x00000000589cb000 nid=0x13a0 runnable [0x0000000058c0f000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x00000000d64f6448> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x00000000d64f6448> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)

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

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

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

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000005701a800 nid=0xf34 in Object.wait() [0x00000000583ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6186bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000d6186bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x0000000056fd2800 nid=0xde0 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000001c82000 nid=0x7d4 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000001c83800 nid=0x1100 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000001c85800 nid=0x163c runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000001c87800 nid=0xbd4 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x0000000058f6d800 nid=0x560 waiting on condition

JNI global references: 12


Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x000000005701ffa8 (object 0x00000000d6451490, a java.lang.Object),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x0000000057021448 (object 0x00000000d64514a0, a java.lang.Object),
which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.matt.message.thread.DeadThread$2.run(DeadThread.java:45)
- waiting to lock <0x00000000d6451490> (a java.lang.Object)
- locked <0x00000000d64514a0> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at com.matt.message.thread.DeadThread$1.run(DeadThread.java:25)
- waiting to lock <0x00000000d64514a0> (a java.lang.Object)
- locked <0x00000000d6451490> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.


C:\Users\Administrator>

 

$flag 上一页 下一页