Java死锁

多线程环境下,相互或循环持有对方需要的资源,从而导致死锁。


相互持有并等待
循环持有并等待

解决方案:
事前:避免相互持有对方的锁,或者循环持有下一个所需要的锁的情况。尽量将某个资源限制在线程内部,避免资源争抢。
事中:找出死锁并强制其中一个退出,即可打破死锁状态
事后:优化代码,如:优化锁顺序,粗化锁粒度等。

下面,手写一个最简单的死锁。

package com.zm.demo.core.concurrent;

import static java.lang.System.out;

/**
 * Created by Administrator on 2020/2/29.
 */
public class DeadLock {

    public static void main(String[]args){

        String source1= "source1";
        String source2= "source2";
        Thread t1 = new Thread(()->{
            //持有source1
            synchronized (source1){
                out.println("t1 lock " + source1);
                //申请source2
                synchronized (source2){
                    out.println("t1 lock " + source2);
                }
            }
        });

        Thread t2 = new Thread(()->{
            //持有source2
            synchronized (source2){
                out.println(" t2 lock " + source2);
                //申请source1
                synchronized (source1){
                    out.println("t2 lock " + source1);
                }
            }
        });

        t1.start();
        t2.start();


    }
}

使用jstack命令查看进程栈信息,可以找出死锁状态的线程。

#找出当前环境下进程信息
D:\git\zm\demo>jps 
10704 AppMain
6864 Launcher
9624
5100 Jps
9212 RemoteMavenServer

#打印进程栈信息
D:\git\zm\demo>jstack 10704
2020-02-29 19:13:21
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.91-b14 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x00000000021ff000 nid=0x418 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" #12 prio=5 os_prio=0 tid=0x00000000098dd000 nid=0x628 waiting for monitor entry [0x000000000b23f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.zm.demo.core.concurrent.DeadLock.lambda$main$1(DeadLock.java:27)
        - waiting to lock <0x00000000d5cf5838> (a java.lang.String)
        - locked <0x00000000d5cf5870> (a java.lang.String)
        at com.zm.demo.core.concurrent.DeadLock$$Lambda$2/1023892928.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

"Thread-0" #11 prio=5 os_prio=0 tid=0x00000000098dc000 nid=0x1528 waiting for monitor entry [0x0000000009a4f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.zm.demo.core.concurrent.DeadLock.lambda$main$0(DeadLock.java:18)
        - waiting to lock <0x00000000d5cf5870> (a java.lang.String)
        - locked <0x00000000d5cf5838> (a java.lang.String)
        at com.zm.demo.core.concurrent.DeadLock$$Lambda$1/1831932724.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

"Monitor Ctrl-Break" #10 daemon prio=5 os_prio=0 tid=0x000000000965d000 nid=0x280c runnable [0x000000000a6cf000]
   java.lang.Thread.State: RUNNABLE
        at java.net.DualStackPlainSocketImpl.accept0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
        at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
        at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
        - locked <0x00000000d5df5730> (a java.net.SocksSocketImpl)
        at java.net.ServerSocket.implAccept(ServerSocket.java:545)
        at java.net.ServerSocket.accept(ServerSocket.java:513)
        at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
        at java.lang.Thread.run(Thread.java:745)

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

"C1 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x0000000009551800 nid=0x14b4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

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

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

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

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000008199800 nid=0x2460 in Object.wait() [0x000000000952e000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5c08ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x00000000d5c08ee0> (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=2 tid=0x0000000008152800 nid=0x233c in Object.wait() [0x000000000932f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5c06b50> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x00000000d5c06b50> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x000000000814b000 nid=0x2b4c runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000000000224d000 nid=0x2a34 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000224e800 nid=0x21f4 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002250000 nid=0x1604 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002251800 nid=0x1aec runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000000960a800 nid=0x1a78 waiting on condition

JNI global references: 317


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x0000000008156898 (object 0x00000000d5cf5838, a java.lang.String),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x0000000008159338 (object 0x00000000d5cf5870, a java.lang.String),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at com.zm.demo.core.concurrent.DeadLock.lambda$main$1(DeadLock.java:27)
        - waiting to lock <0x00000000d5cf5838> (a java.lang.String)
        - locked <0x00000000d5cf5870> (a java.lang.String)
        at com.zm.demo.core.concurrent.DeadLock$$Lambda$2/1023892928.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
"Thread-0":
        at com.zm.demo.core.concurrent.DeadLock.lambda$main$0(DeadLock.java:18)
        - waiting to lock <0x00000000d5cf5870> (a java.lang.String)
        - locked <0x00000000d5cf5838> (a java.lang.String)
        at com.zm.demo.core.concurrent.DeadLock$$Lambda$1/1831932724.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

最后几行,很明显的,直接打印出死锁信息了。而且直接打印出死锁的代码位置,我们可以很方便的找到代码并进行优化。
Thread-0持有0x00000000d5cf5838,等待0x00000000d5cf5870
同时
Thread-1持有0x00000000d5cf5870,等待0x00000000d5cf5838
因此,产生死锁。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,125评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,293评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,054评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,077评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,096评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,062评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,988评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,817评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,266评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,486评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,646评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,375评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,974评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,621评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,642评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,538评论 2 352