java线程基础巩固---多线程死锁分析,案例介绍

之前已经学习了关于同步锁的知识,但是在实际编写多线程程序时可能会存在死锁的情况,所以这次来模拟一下死锁,并且学会用一个命令来确认是否程序已经出现死锁了,下面开始:

首先新建两个类:

此时当然得到OtherService类中增加这个s1()方法喽,如下:

目前是DeadLock调用了OtherService的方法,这时反过了,让OtherService去调用DeadLock类的方法,所以代码如下:

所以此时在DeadLock中新建一个m2()方法,如下:

此时编写测试代码来测试一下,首先构造相关依赖的关系,如下:

接下来新建两个线程然后发起方法调用,具体如下:

接下来编译运行:

执行了一段之后程序卡住了,而且进程一直没有结束,其实这就是典型死锁原因造成,用什么方式来以查看到这个死锁的状态呢?其实可以通过一个命令行可以查询,具体如下:

先用"jps"命令查看当前程序的进程号,如下:

然后用"jstack"命令查看该进程的线程情况,如下:

xiongweideMacBook-Pro:wechat_jump_game xiongwei$ jstack 99374
2018-02-04 15:13:00
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode):

"Attach Listener" #12 daemon prio=9 os_prio=31 tid=0x00007fe88404b000 nid=0x1107 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

"Thread-1" #10 prio=5 os_prio=31 tid=0x00007fe883087000 nid=0x4d03 waiting for monitor entry [0x00007000061e1000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.javaconcurrency.synchronized4.DeadLock.m2(DeadLock.java:20)
    - waiting to lock <0x0000000795780bb0> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.OtherService.s2(OtherService.java:21)
    - locked <0x000000079577c6d8> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.DeadLockTest$2.run(DeadLockTest.java:22)

"Thread-0" #9 prio=5 os_prio=31 tid=0x00007fe88381a000 nid=0x4b03 waiting for monitor entry [0x00007000060de000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.javaconcurrency.synchronized4.OtherService.s1(OtherService.java:14)
    - waiting to lock <0x000000079577c6d8> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.DeadLock.m1(DeadLock.java:14)
    - locked <0x0000000795780bb0> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.DeadLockTest$1.run(DeadLockTest.java:13)

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

"C1 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007fe884053000 nid=0x4503 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007fe884010000 nid=0x4303 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007fe88380d000 nid=0x4103 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

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

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007fe88281e000 nid=0x3003 in Object.wait() [0x00007000058c6000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000795586af8> (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 <0x0000000795586af8> (a java.lang.ref.Reference$Lock)

"VM Thread" os_prio=31 tid=0x00007fe884006800 nid=0x2e03 runnable 

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007fe884003800 nid=0x2607 runnable 

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007fe884004000 nid=0x2803 runnable 

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007fe884004800 nid=0x2a03 runnable 

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007fe884005000 nid=0x2c03 runnable 

"VM Periodic Task Thread" os_prio=31 tid=0x00007fe883809000 nid=0x4903 waiting on condition 

JNI global references: 6


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x00007fe882822a18 (object 0x0000000795780bb0, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x00007fe882823d58 (object 0x000000079577c6d8, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
    at com.javaconcurrency.synchronized4.DeadLock.m2(DeadLock.java:20)
    - waiting to lock <0x0000000795780bb0> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.OtherService.s2(OtherService.java:21)
    - locked <0x000000079577c6d8> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.DeadLockTest$2.run(DeadLockTest.java:22)
"Thread-0":
    at com.javaconcurrency.synchronized4.OtherService.s1(OtherService.java:14)
    - waiting to lock <0x000000079577c6d8> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.DeadLock.m1(DeadLock.java:14)
    - locked <0x0000000795780bb0> (a java.lang.Object)
    at com.javaconcurrency.synchronized4.DeadLockTest$1.run(DeadLockTest.java:13)

Found 1 deadlock.

xiongweideMacBook-Pro:wechat_jump_game xiongwei$ 

其中可以在这个命令的最后语句中看到如下提示:

那具体原因是怎么造成的呢?从命令行中的提示就可以分析得出:

所以如果实际项目中就可以通过这个命令去查询死锁的问题。

posted on 2018-01-20 14:49  cexo  阅读(461)  评论(0编辑  收藏  举报

导航