码迷,mamicode.com
首页 > 编程语言 > 详细

2018-07-22期 Java死锁小案例编程

时间:2018-07-23 11:19:21      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:mon   block   patch   listener   zed   thread   nis   form   interrupt   

1、死锁案例

public class DeadLock {

final Object lockA = new Object();

public static void main(String[] args) {

DeadLock demo = new DeadLock();

demo.startLock();

}

public void startLock() {

ThreadA a = new ThreadA(lockA, lockB);

ThreadB b = new ThreadB(lockA, lockB);

a.start();

b.start();

}

}

class ThreadA extends Thread {

private Object lockA = null;

private Object lockB = null;

public ThreadA(Object a, Object b) {

this.lockA = a;

this.lockB = b;

}

public void run() {

synchronized (lockA) {

System.out.println("---- Thread A: locked A");

try {

sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (lockB) {

System.out.println("---- Thread A: locked B");

}

}

System.out.println("---- Thread A: Finished");

}

}

class ThreadB extends Thread {

private Object lockA = null;

private Object lockB = null;

public ThreadB(Object a, Object b) {

this.lockA = a;

this.lockB = b;

}

public void run() {

synchronized (lockB) {

System.out.println("---- Thread B: locked B");

try {

sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (lockA) {

System.out.println("Tread B: locked A");

}

}

System.out.println("---- Thread B: Finished");

}

}

2、死锁分析

打印Thread Dump

linux 环境kill -3 pid 或者jstack

windows环境 ctrl+fn+b

dump信息如下:

D:\test\deadlock>java DeadLock

---- Thread A: locked A

---- Thread B: locked B

2018-07-14 17:45:52

Full thread dump Java HotSpot(TM) Client VM (24.65-b04 mixed mode, sharing):

"DestroyJavaVM" prio=6 tid=0x02c4bc00 nid=0x1548 waiting on condition [0x00000000]

  java.lang.Thread.State: RUNNABLE

"Thread-1" prio=6 tid=0x02d2b400 nid=0x2b24 waiting for monitor entry [0x054bf000]

  java.lang.Thread.State: BLOCKED (on object monitor)

       at ThreadB.run(DeadLock.java:64)

       - waiting to lock <0x252811c0> (a java.lang.Object)

       - locked <0x252811c8> (a java.lang.Object)

"Thread-0" prio=6 tid=0x02d2ac00 nid=0x3640 waiting for monitor entry [0x0542f000]

  java.lang.Thread.State: BLOCKED (on object monitor)

       at ThreadA.run(DeadLock.java:37)

       - waiting to lock <0x252811c8> (a java.lang.Object)

       - locked <0x252811c0> (a java.lang.Object)

"Service Thread" daemon prio=6 tid=0x02cdd800 nid=0x4138 runnable [0x00000000]

  java.lang.Thread.State: RUNNABLE

"C1 CompilerThread0" daemon prio=10 tid=0x02cdc800 nid=0x2324 waiting on condition [0x00000000]

  java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x02cdac00 nid=0x22dc runnable [0x00000000]

  java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x02d04800 nid=0x2fb0 waiting on condition [0x00000000]

  java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x02c70000 nid=0x708 in Object.wait() [0x050cf000]

  java.lang.Thread.State: WAITING (on object monitor)

       at java.lang.Object.wait(Native Method)

       - waiting on <0x25200fc8> (a java.lang.ref.ReferenceQueue$Lock)

       at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)

       - locked <0x25200fc8> (a java.lang.ref.ReferenceQueue$Lock)

       at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)

       at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" daemon prio=10 tid=0x02c6e800 nid=0xf40 in Object.wait() [0x0503f000]

  java.lang.Thread.State: WAITING (on object monitor)

       at java.lang.Object.wait(Native Method)

       - waiting on <0x25200db0> (a java.lang.ref.Reference$Lock)

       at java.lang.Object.wait(Object.java:503)

       at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)

       - locked <0x25200db0> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=10 tid=0x02c6d400 nid=0x1150 runnable

"VM Periodic Task Thread" prio=10 tid=0x02d16400 nid=0x3130 waiting on condition

JNI global references: 111

Found one Java-level deadlock:

=============================

"Thread-1":

 waiting to lock monitor 0x02c73be4 (object 0x252811c0, a java.lang.Object),

 which is held by "Thread-0"

"Thread-0":

 waiting to lock monitor 0x02c75704 (object 0x252811c8, a java.lang.Object),

 which is held by "Thread-1"

Java stack information for the threads listed above:

===================================================

"Thread-1":

       at ThreadB.run(DeadLock.java:64)

       - waiting to lock <0x252811c0> (a java.lang.Object)

       - locked <0x252811c8> (a java.lang.Object)

"Thread-0":

       at ThreadA.run(DeadLock.java:37)

       - waiting to lock <0x252811c8> (a java.lang.Object)

       - locked <0x252811c0> (a java.lang.Object)

Found 1 deadlock.

Heap

def new generation   total 4928K, used 867K [0x25200000, 0x25750000, 0x2a750000)

 eden space 4416K,  19% used [0x25200000, 0x252d8fe0, 0x25650000)

 from space 512K,   0% used [0x25650000, 0x25650000, 0x256d0000)

 to   space 512K,   0% used [0x256d0000, 0x256d0000, 0x25750000)

tenured generation   total 10944K, used 0K [0x2a750000, 0x2b200000, 0x35200000)

  the space 10944K,   0% used [0x2a750000, 0x2a750000, 0x2a750200, 0x2b200000)

compacting perm gen  total 12288K, used 157K [0x35200000, 0x35e00000, 0x39200000)

  the space 12288K,   1% used [0x35200000, 0x35227730, 0x35227800, 0x35e00000)

   ro space 10240K,  44% used [0x39200000, 0x3967a688, 0x3967a800, 0x39c00000)

   rw space 12288K,  52% used [0x39c00000, 0x3a254170, 0x3a254200, 0x3a800000)


2018-07-22期 Java死锁小案例编程

标签:mon   block   patch   listener   zed   thread   nis   form   interrupt   

原文地址:http://blog.51cto.com/2951890/2148846

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!