Using Jstack to detect deadlocks under Linux in Java

Source: Internet
Author: User

First, write a deadlock program

1  Packagedeadlock;2 3  Public classTestjstack {4     Final StaticObject resource_1 =NewObject ();5     Final StaticObject resource_2 =NewObject ();6 7      Public Static voidMain (string[] args) {8Thread T1 =NewThread ("T1") {9              Public voidrun () {Ten                 synchronized(resource_1) { One                     Try { AThread.Sleep (3000); -}Catch(interruptedexception e) { -                     } theSystem.out.println ("Locked Resource_1"); -                     synchronized(resource_2) { -SYSTEM.OUT.PRINTLN ("Thread T1 done.")); -                     } +                 } -             } +         }; A  atThread t2 =NewThread ("T2") { -              Public voidrun () { -                 synchronized(resource_2) { -System.out.println ("Locked resource_2"); -                     synchronized(resource_1) { -System.out.println ("Thread T2 done.")); in                     } -                 } to             } +         }; - T1.start (); the T2.start (); *     } $}

The result of the program operation is:

Lock Resource_2lock Resource_1

Next, enter the JSP in the terminal to view the currently running Java program:

7480 testjstack13420 Jps

Gets the Testjstack process ID of 7480. Then use the command:

Jstack-l 7480 >deadlock.jstack

Put the Jstack detection results into the file deadlock.jstack. Use Vim to view the file:

Java.lang.Thread.State:RUNNABLE Locked ownable synchronizers:-None Java.lang.Thread.State:RUNNABLE Java.lang.Thread.State:BLOCKED (on object monitor) at testjstack$
    2.run (testjstack.java:29)        -Waiting to lock <0x8c087670>(a java.lang.Object)-Locked <0x8c087678>(a Java.lang.Object) Java.lang.Thread.State:BLOCKED (on Object Monitor) at testjstack$1.run (testjstack.java:15) Locked ownable synchronizers:-None Java.lang.Thread.State:RUNNABLE Locked ownable synchronizers:-None Java.lang.Thread.State:RUNNABLE Locked ownable Synchronizers:java.lang.Thread.State:RUNNABLE Java.lan G.thread.state:waiting (on object monitor) at java.lang.Object.wait (Native Method)-Locked <0x8c050b30>(a java.lang.ref.referencequeue$lock) Locked ownable synchronizers:-None-Waiting on <0x8c050a30>(a java.lang.ref.reference$lock) at java.lang.Object.wait (Object.java:485)        -Locked <0x8c050a30>(a java.lang.ref.reference$lock) Locked ownable synchronizers:"VM Thread" prio=10 tid=0x0807e800 nid=0x260cRunnablejni Global References:576Found One Java-Level Deadlock:============================= "T2": Which is held by"T1" "T1": Which is held by"T2"Java Stack Information forThe threads listed above:=================================================== "T2": at testjstack$2.run (testjstack.java:29)        -Waiting to lock <0x8c087670>(a java.lang.Object)-Locked <0x8c087678>(a java.lang.Object)"T1": at testjstack$1.run (testjstack.java:15)        -Waiting to lock <0x8c087678>(a java.lang.Object)-Locked <0x8c087670>(a java.lang.Object) Found1 deadlock.

Using Jstack to detect deadlocks under Linux in Java

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.