您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Java中怎么實(shí)現(xiàn)死鎖與活鎖,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
具體如下:
順序死鎖:過度加鎖,導(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ī)性
看完上述內(nèi)容,你們對(duì)Java中怎么實(shí)現(xiàn)死鎖與活鎖有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。