Java Deadlock and command detection

Source: Internet
Author: User

Java each object has a lock, the current process uses the object lock 1, does not release the lock, and want to get another lock of object 2, and the object Lock 2 is held by another thread, not released, it is easy to deadlock

1. Deadlock instances

public class Deadlocktest {private static object Object1 = new Object ();p rivate static Object object2 = new Object ();p Ubli c static void Main (string[] args) {new Thread (new Runnable () {@Overridepublic void run () {synchronized (Object1) {SYSTEM.O UT.PRINTLN ("Thread 1 gets object1 lock ..."); try {thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();} Synchronized (object2) {System.out.println ("Thread 1 gets object2 lock ...");}}}). Start (); New Thread (New Runnable () {@Overridepublic void run () {synchronized (object2) {System.out.println (" Thread 2 gets object2 lock ... "); synchronized (Object1) {System.out.println (" Thread 2 gets Object1 lock ... ");}}). Start (); System.out.println ("Main thread execution complete ...");}}

Analysis: Thread 1 holds object1 lock, but also want to get object2 lock, and at this time thread 2 holds OBJECT2 lock is not released, and want to get object1 lock, which caused the deadlock.

2. View Deadlocks by command

Prerequisites: Enter the bin directory of the JDK

2.1 JPS

JPS commands to view Java-related processes

2.2 Jstack

Jstack: View analysis of specific process executions

Run the above program, execute the JPS command, and find the process number of the above code is 14888

Then execute the jstack-l PID (process number)

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 on waiting [condition 0]
Java.lang.Thread.State:RUNNABLE

Locked ownable Synchronizers:
-None

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

Locked ownable Synchronizers:
-None

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

Locked ownable Synchronizers:
-None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000001c0b9800 nid=0x2f08 on waiting [condition 0]
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.

As can be seen from the above results, two threads are in blocked blocking state, and it is clear that a deadlock has been found;

Java Deadlock and command detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.