亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 ReentrantLock 時出現死鎖

使用 ReentrantLock 時出現死鎖

守候你守候我 2022-07-06 19:10:42
我使用 ReentrantLock 實現生產者消費者問題public class Processor {   Lock lock =  new ReentrantLock(true);   Condition condn = lock.newCondition();    public void produce() throws InterruptedException{       lock.lock();            System.out.println("inside producer method");            condn.await();            System.out.println("thread again wakeup");        lock.unlock();    }    public void consume() throws InterruptedException{          lock.lock();            Thread.sleep(1000);            condn.signal();            System.out.println("will i ever be ok ");            lock.unlock();    }}ReentrantLock 同步了兩種方法,但有時進程會死鎖o/p 在生產者方法中我會不會沒事在運行堆轉儲時Reference Handler" #2 daemon prio=10 os_prio=31 cpu=0.26ms elapsed=540.35s tid=0x00007fd5f186f800 nid=0x4603 等待條件 [0x0000700002357000] java.lang.Thread.State: RUNNABLE    at java.lang.ref.Reference.waitForReferencePendingList([email protected]/Native Method)    at java.lang.ref.Reference.processPendingReferences([email protected]/Reference.java:241)    at java.lang.ref.Reference$ReferenceHandler.run([email protected]/Reference.java:213)"Finalizer" #3 daemon prio=8 os_prio=31 cpu=0.67ms elapsed=540.35s tid=0x00007fd5f1883000 nid=0x4303 in Object.wait()  [0x000070000245a000]   java.lang.Thread.State: WAITING (on object monitor)    at java.lang.Object.wait([email protected]/Native Method)    - waiting on <0x0000000787f08f80> (a java.lang.ref.ReferenceQueue$Lock)    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:155)    - waiting to re-lock in wait() <0x0000000787f08f80> (a java.lang.ref.ReferenceQueue$Lock)    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:176)    at java.lang.ref.Finalizer$FinalizerThread.run([email protected]/Finalizer.java:170)"Signal Dispatcher" #4 daemon prio=9 os_prio=31 cpu=0.34ms elapsed=540.27s tid=0x00007fd5f1884000 nid=0x3903 waiting on condition  [0x0000000000000000]   java.lang.Thread.State: RUNNABLE為什么即使在我使用釋放鎖后它也會死鎖    condn.signal();
查看完整描述

2 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

如果消費者consume先調用然后生產者調用produce,那么生產者將錯過signal并卡住。



查看完整回答
反對 回復 2022-07-06
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

你可以用Phaser它。關鍵是Phaser制片人問“我錯過了一些消費嗎?” 如果不是,它正在等待,如果是,它會立即繼續進行。這是一個例子:


public class Processor {

   private final Phaser phaser = new Phaser(1);

   private volatile int lastPhaseId = 0;


    public void produce() throws InterruptedException{

            System.out.println("inside producer method");

            lastPhaseId = phaser.awaitAdvance(lastPhaseId);

            System.out.println("thread again wakeup");

    }


    public void consume() throws InterruptedException{

            Thread.sleep(1000);

            phaser.arrive();

            System.out.println("will i ever be ok ");

    }

}

這適用于單個消費者的情況。如果假設有多個消費者,則phase.arrive()需要互斥訪問和/或注冊方相應更改(取決于您需要什么語義)


查看完整回答
反對 回復 2022-07-06
  • 2 回答
  • 0 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號