<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 多Product多Consumer之間的通訊導致出現程序假死的原因分析 繼續上篇文章,我們來吧單個生產者消費者改成多個生產者消費者,這里使用java8的Stream,代碼如下: ```java /** * @program: ThreadDemo * @description: 線程通信(生產者-消費者) * @author: hs96.cn@Gmail.com * @create: 2020-09-06 */ public class ProduceConsumerVersion2 { private int i = 0; private final Object LOCK = new Object(); /** * 是否已經消費 */ private volatile boolean isProduced = false; public void produce() { synchronized (LOCK) { if (isProduced) { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { System.out.println("P->" + (++i)); LOCK.notify(); isProduced = true; } } } public void consume() { synchronized (LOCK) { if (isProduced) { System.out.println("C->" + i); LOCK.notify(); isProduced = false; } else { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { ProduceConsumerVersion2 pc = new ProduceConsumerVersion2(); // 多個生產者、消費者線程運行時,會產生一些問題 Stream.of("P1", "P2").forEach(n -> new Thread(() -> { while (true) { pc.produce(); } }, n).start() ); Stream.of("C1", "C2").forEach(n -> new Thread(() -> { while (true) { pc.consume(); } }, n).start() ); } } ``` 運行效果如下: ![](https://img.kancloud.cn/90/c5/90c5a2301ddff7bec48b437b1fc155ce_960x231.gif) 可以看到阻塞了,那么是不是有死鎖了呢,jstack來查一下: ![](https://img.kancloud.cn/94/90/9490ebdde365cc00325da4a49425e6e6_960x231.gif) 可以看到沒有死鎖,那是為什么會卡住呢?下面增加一些調試語句用來定位跟蹤,我把代碼貼出來,有興趣的朋友在大腦里分析一下,本地再執行一下驗證自己的想法,代碼如下: ```java public class ProduceConsumerVersion2 { private int i = 0; private final Object LOCK = new Object(); /** * 是否已經消費 */ private volatile boolean isProduced = false; public void produce() { synchronized (LOCK) { if (isProduced) { try { System.out.println(Thread.currentThread().getName() + "wait了"); LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { i++; System.out.println(Thread.currentThread().getName() + "生產了->" + i); LOCK.notify(); System.out.println(Thread.currentThread().getName() + "notify了" + i); isProduced = true; } } } public void consume() { synchronized (LOCK) { if (isProduced) { System.out.println(Thread.currentThread().getName() + "消費了->" + i); LOCK.notify(); System.out.println(Thread.currentThread().getName() + "notify了"); isProduced = false; } else { try { System.out.println(Thread.currentThread().getName() + "wait了"); LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { ProduceConsumerVersion2 pc = new ProduceConsumerVersion2(); // 多個生產者、消費者線程運行時,會產生一些問題 Stream.of("P1", "P2").forEach(n -> new Thread(() -> { while (true) { pc.produce(); } }, n).start() ); Stream.of("C1", "C2").forEach(n -> new Thread(() -> { while (true) { pc.consume(); } }, n).start() ); } } ``` 運行效果如下: ![](https://img.kancloud.cn/df/e6/dfe6c46c69028ee59918bb77be4ad3e7_1029x276.gif) 可以看到P1,C1,P2,C2都wait了,然后程序就假死了,這是什么原因呢? 這里我就不一步一步分析了,這里只需要明白一個道理即可,當有多個wait的時候,notify是隨機的。這個我們后續再探討,先吃個飯~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看