Java中常见死锁与活锁的实例详解
程序员文章站
2024-03-05 09:08:42
本文介绍了java中常见死锁与活锁的实例详解,分享给大家,具体如下:
顺序死锁:过度加锁,导致由于执行顺序的原因,互相持有对方正在等待的锁
资源死锁:多个线...
本文介绍了java中常见死锁与活锁的实例详解,分享给大家,具体如下:
- 顺序死锁:过度加锁,导致由于执行顺序的原因,互相持有对方正在等待的锁
- 资源死锁:多个线程在相同的资源上发生等待
由于调用顺序而产生的死锁
public class test { object leftlock = new object(); object rightlock = new object(); public static void main(string[] args) { final test test = new test(); thread a = new thread(new runnable() { @override public void run() { int i=0; while (i<10) { test.leftright(); i++; } } },"athread"); thread b = new thread(new runnable() { @override public void run() { int i=0; while (i<10) { test.rightleft(); i++; } } },"bthread"); a.start(); b.start(); } public void leftright(){ synchronized (leftlock){ system.out.println(thread.currentthread().getname()+":leftright:get left"); synchronized (rightlock){ system.out.println(thread.currentthread().getname()+":leftright:get right"); } } } public void rightleft(){ synchronized (rightlock){ system.out.println(thread.currentthread().getname()+":rightleft: get right"); synchronized (leftlock){ system.out.println(thread.currentthread().getname()+":rightleft: get left"); } } } }
运行后输出如下
athread:leftright:get left
bthread:rightleft: get right
可以通过jstack发现死锁的痕迹
"bthread" prio=5 tid=0x00007fabb2001000 nid=0x5503 waiting for monitor entry [0x000000011d54b000] java.lang.thread.state: blocked (on object monitor) at main.locktest.test.rightleft(test.java:52) - waiting to lock <0x00000007aaee5748> (a java.lang.object) - locked <0x00000007aaee5758> (a java.lang.object) at main.locktest.test$2.run(test.java:30) at java.lang.thread.run(thread.java:745) locked ownable synchronizers: - none "athread" prio=5 tid=0x00007fabb2801000 nid=0x5303 waiting for monitor entry [0x000000011d448000] java.lang.thread.state: blocked (on object monitor) at main.locktest.test.leftright(test.java:43) - waiting to lock <0x00000007aaee5758> (a java.lang.object) - locked <0x00000007aaee5748> (a java.lang.object) at main.locktest.test$1.run(test.java:19) at java.lang.thread.run(thread.java:745) locked ownable synchronizers: - none
可以看到bthread持有锁0x00000007aaee5758,同时等待0x00000007aaee5748,然而恰好athread持有锁0x00000007aaee5748并等待0x00000007aaee5758,从而形成了死锁
线程饥饿死锁
public class executorlock { private static executorservice single=executors.newsinglethreadexecutor(); public static class anothercallable implements callable<string>{ @override public string call() throws exception { system.out.println("in anothercallable"); return "annother success"; } } public static class mycallable implements callable<string>{ @override public string call() throws exception { system.out.println("in mycallable"); future<string> submit = single.submit(new anothercallable()); return "success:"+submit.get(); } } public static void main(string[] args) throws executionexception, interruptedexception { mycallable task = new mycallable(); future<string> submit = single.submit(task); system.out.println(submit.get()); system.out.println("over"); single.shutdown(); } }
执行的输出只有一行
in mycallable
通过jstack观察可以看到如下
"main" prio=5 tid=0x00007fab3f000000 nid=0x1303 waiting on condition [0x0000000107d63000] java.lang.thread.state: waiting (parking) at sun.misc.unsafe.park(native method) - parking to wait for <0x00000007aaeed1d8> (a java.util.concurrent.futuretask) at java.util.concurrent.locks.locksupport.park(locksupport.java:186) at java.util.concurrent.futuretask.awaitdone(futuretask.java:425) at java.util.concurrent.futuretask.get(futuretask.java:187) at main.locktest.executorlock.main(executorlock.java:32) at sun.reflect.nativemethodaccessorimpl.invoke0(native method) at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) at java.lang.reflect.method.invoke(method.java:606) at com.intellij.rt.execution.application.appmain.main(appmain.java:140) locked ownable synchronizers: - none .. "pool-1-thread-1" prio=5 tid=0x00007fab3f835800 nid=0x5303 waiting on condition [0x00000001199ee000] java.lang.thread.state: waiting (parking) at sun.misc.unsafe.park(native method) - parking to wait for <0x00000007ab0f8698> (a java.util.concurrent.futuretask) at java.util.concurrent.locks.locksupport.park(locksupport.java:186) at java.util.concurrent.futuretask.awaitdone(futuretask.java:425) at java.util.concurrent.futuretask.get(futuretask.java:187) at main.locktest.executorlock$mycallable.call(executorlock.java:26) at main.locktest.executorlock$mycallable.call(executorlock.java:20) at java.util.concurrent.futuretask.run(futuretask.java:262) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) at java.lang.thread.run(thread.java:745) locked ownable synchronizers: - <0x00000007aaeed258> (a java.util.concurrent.threadpoolexecutor$worker)
主线程在等待一个futuretask完成,而线程池中一个线程也在等待一个futuretask完成。
从代码实现可以看到,主线程往线程池中扔了一个任务a,任务a又往同一个线程池中扔了一个任务b,并等待b的完成,由于线程池中只有一个线程,这将导致b会被停留在阻塞队列中,而a还得等待b的完成,这也就是互相等待导致了死锁的反生
这种由于正在执行的任务线程都在等待其它工作队列中的任务而阻塞的现象称为 线程饥饿死锁
活锁
并未产生线程阻塞,但是由于某种问题的存在,导致无法继续执行的情况。
1、消息重试。当某个消息处理失败的时候,一直重试,但重试由于某种原因,比如消息格式不对,导致解析失败,而它又被重试
这种时候一般是将不可修复的错误不要重试,或者是重试次数限定
2、相互协作的线程彼此响应从而修改自己状态,导致无法执行下去。比如两个很有礼貌的人在同一条路上相遇,彼此给对方让路,但是又在同一条路上遇到了。互相之间反复的避让下去
这种时候可以选择一个随机退让,使得具备一定的随机性
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。