Java中常見死鎖與活鎖的實(shí)例詳解
本文介紹了Java中常見死鎖與活鎖的實(shí)例詳解,分享給大家,具體如下:
- 順序死鎖:過度加鎖,導(dǎo)致由于執(zhí)行順序的原因,互相持有對(duì)方正在等待的鎖
- 資源死鎖:多個(gè)線程在相同的資源上發(fā)生等待
由于調(diào)用順序而產(chǎn)生的死鎖
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");
}
}
}
}
運(yùn)行后輸出如下
aThread:leftRight:get left
bThread:rightleft: get right
可以通過jstack發(fā)現(xiàn)死鎖的痕跡
"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,同時(shí)等待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();
}
}
執(zhí)行的輸出只有一行
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)
主線程在等待一個(gè)FutureTask完成,而線程池中一個(gè)線程也在等待一個(gè)FutureTask完成。
從代碼實(shí)現(xiàn)可以看到,主線程往線程池中扔了一個(gè)任務(wù)A,任務(wù)A又往同一個(gè)線程池中扔了一個(gè)任務(wù)B,并等待B的完成,由于線程池中只有一個(gè)線程,這將導(dǎo)致B會(huì)被停留在阻塞隊(duì)列中,而A還得等待B的完成,這也就是互相等待導(dǎo)致了死鎖的反生
這種由于正在執(zhí)行的任務(wù)線程都在等待其它工作隊(duì)列中的任務(wù)而阻塞的現(xiàn)象稱為 線程饑餓死鎖
活鎖
并未產(chǎn)生線程阻塞,但是由于某種問題的存在,導(dǎo)致無法繼續(xù)執(zhí)行的情況。
1、消息重試。當(dāng)某個(gè)消息處理失敗的時(shí)候,一直重試,但重試由于某種原因,比如消息格式不對(duì),導(dǎo)致解析失敗,而它又被重試
這種時(shí)候一般是將不可修復(fù)的錯(cuò)誤不要重試,或者是重試次數(shù)限定
2、相互協(xié)作的線程彼此響應(yīng)從而修改自己狀態(tài),導(dǎo)致無法執(zhí)行下去。比如兩個(gè)很有禮貌的人在同一條路上相遇,彼此給對(duì)方讓路,但是又在同一條路上遇到了?;ハ嘀g反復(fù)的避讓下去
這種時(shí)候可以選擇一個(gè)隨機(jī)退讓,使得具備一定的隨機(jī)性
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何調(diào)用chatGPT實(shí)現(xiàn)代碼機(jī)器人
最近c(diǎn)hatGPT也是非常的火爆,相信大家都看到了,現(xiàn)在提供一種Java調(diào)用chatGPT的方法,我們主要通過兩個(gè)工具來實(shí)現(xiàn),一就是httpclient,二就是hutool,你覺得那種好理解你就用那種即可,今天通過本文給大家分享調(diào)用chatGPT實(shí)現(xiàn)代碼機(jī)器人,感興趣的朋友一起看看吧2022-12-12
JavaGUI事件監(jiān)聽機(jī)制超詳細(xì)講解
Java事件監(jiān)聽器是由事件類和監(jiān)聽接口組成,自定義一個(gè)事件前,必須提供一個(gè)事件的監(jiān)聽接口以及一個(gè)事件類。JAVA中監(jiān)聽接口是繼承java.util.EventListener的類,事件類繼承java.util.EventObject的類2023-03-03
java使用Jdom實(shí)現(xiàn)xml文件寫入操作實(shí)例
這篇文章主要介紹了java使用Jdom實(shí)現(xiàn)xml文件寫入操作的方法,以完整實(shí)例形式分析了Jdom針對(duì)XML文件寫入操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
spring boot + mybatis如何實(shí)現(xiàn)數(shù)據(jù)庫(kù)的讀寫分離
這篇文章主要給大家介紹了關(guān)于spring boot + mybatis如何實(shí)現(xiàn)數(shù)據(jù)庫(kù)的讀寫分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Spring boot項(xiàng)目中異常攔截設(shè)計(jì)和處理詳解
這篇文章主要介給大家紹了關(guān)于Spring boot項(xiàng)目中異常攔截設(shè)計(jì)和處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧2018-12-12
springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot如何優(yōu)雅的實(shí)現(xiàn)重試功能
這篇文章主要詳細(xì)介紹了SpringBoot如何優(yōu)雅的實(shí)現(xiàn)重試功能的步驟詳解,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的同學(xué)可以借鑒閱讀2023-06-06
SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

