java死锁示例

public class TestService {
    public static void main(String[] args) {
        Thread one=new Thread("one"){
            public void run() {
                synchronized(TestService.class){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (args) {
                        System.out.println("one ok");
                    }
                }
            };
        };
        
        Thread two=new Thread("two"){
            public void run() {
                synchronized (args) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized(TestService.class){
                        System.out.println("two ok");
                    }
                }
            };
        };
        one.start();
        two.start();
    }
}




引用

C:\Users\Administrator>jps
3104
7060 TestService
6428 Jps
7180

C:\Users\Administrator>jstack -l 7060
2016-12-16 14:02:15
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):

"DestroyJavaVM" #11 prio=5 os_prio=0 tid=0x0000000001c1e000 nid=0xe24 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"two" #10 prio=5 os_prio=0 tid=0x00000000584ae800 nid=0x1924 waiting for monitor entry [0x0000000058d8f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at testweb.TestService$2.run(TestService.java:35)
        - waiting to lock <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)
        - locked <0x00000000d615b148> (a [Ljava.lang.String;)

   Locked ownable synchronizers:
        - None

"one" #9 prio=5 os_prio=0 tid=0x00000000584ad800 nid=0x1d6c waiting for monitor entry [0x0000000058c4f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at testweb.TestService$1.run(TestService.java:19)
        - waiting to lock <0x00000000d615b148> (a [Ljava.lang.String;)
        - locked <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)

   Locked ownable synchronizers:
        - None

"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x000000005849a800 nid=0xd8c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

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

   Locked ownable synchronizers:
        - None

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

   Locked ownable synchronizers:
        - None

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

   Locked ownable synchronizers:
        - None

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

   Locked ownable synchronizers:
        - None

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000005700c000 nid=0x1e78 in Object.wait() [0x000000005801e000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d6006f58> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x00000000d6006f58> (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=0x0000000057003000 nid=0x1fa4 in Object.wait() [0x0000000057e6f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d6006998> (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 <0x00000000d6006998> (a java.lang.ref.Reference$Lock)

   Locked ownable synchronizers:
        - None

"VM Thread" os_prio=2 tid=0x0000000056ffd000 nid=0x17f0 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000023d3800 nid=0x1d9c runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000023d6000 nid=0x1d08 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x00000000584a4800 nid=0x1e7c waiting on condition

JNI global references: 6


Found one Java-level deadlock:
=============================
"two":
  waiting to lock monitor 0x000000005700a958 (object 0x00000000d61578b0, a java.lang.Class),
  which is held by "one"
"one":
  waiting to lock monitor 0x000000005700bdf8 (object 0x00000000d615b148, a [Ljava.lang.String;),
  which is held by "two"

Java stack information for the threads listed above:
===================================================
"two":
        at testweb.TestService$2.run(TestService.java:35)
        - waiting to lock <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)
        - locked <0x00000000d615b148> (a [Ljava.lang.String;)
"one":
        at testweb.TestService$1.run(TestService.java:19)
        - waiting to lock <0x00000000d615b148> (a [Ljava.lang.String;)
        - locked <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)

Found 1 deadlock.


猜你喜欢

转载自qiyuxi.iteye.com/blog/2345349