<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 使用notifyAll完善多線程下的生產者消費者模型 我們先看一下wait()的代碼注釋: ![](https://img.kancloud.cn/e1/70/e170e77650493fe9113387e016db294b_694x307.png) 使當前線程等待,直到另一個線程調用了這個對象的notify()或notifyAll()方法,換句話說,這個方法是執行了wait(0); 當前線程必須擁有該對象的監視器。線程釋放此監視器的所有權,并等待另一個線程通過調用notify()或notifyAll()方法,讓這個對象監視器上等待的線程被喚醒,然后線程等待,直到它可以重新獲得監視器的所有權并恢復執行。 notify()是通知一個線程喚醒,而notifyAll()是會將wait()在同一個monitor的所有線程都會喚醒,而我們之前出現問題就是因為notify是隨機的,那么我們現在使用notifyAll試一下: ```java /** * @program: ThreadDemo * @description: 優化多線程下的生產者消費者模型,避免多生產者-多消費者造成的程序假死 * @author: hs96.cn@Gmail.com * @create: 2020-09-07 */ public class ProduceConsumerVersion3 { 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(); } } System.out.println(Thread.currentThread().getName() + "->" + (++i)); LOCK.notifyAll(); isProduced = true; } } public void consume() { synchronized (LOCK) { if (!isProduced) { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "->" + i); LOCK.notifyAll(); isProduced = false; } } public static void main(String[] args) { ProduceConsumerVersion3 pc = new ProduceConsumerVersion3(); Stream.of("P1", "P2", "P3").forEach(n -> new Thread(new Runnable() { @Override public void run() { while (true) { pc.produce(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }, n).start()); Stream.of("C1", "C2", "C3", "C4").forEach(n -> new Thread(() -> { while (true) { pc.consume(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }, n).start()); } } ``` 運行效果如下: ![](https://img.kancloud.cn/ce/e5/cee5e08d64338cccf31c0d24ba0dc5bc_1106x331.png) 可以看到有連續生產2個的情況,那么是什么原因導致的呢? 其實仔細想一下,就是p3的生產完之后釋放鎖,然后p2搶到了,就繼續生產了,那么怎么解決呢?讓p2wait到p3生產的數據消費掉即可了,改進代碼如下:‘ ```java /** * @program: ThreadDemo * @description: 優化多線程下的生產者消費者模型,避免多生產者-多消費者造成的程序假死 * @author: hs96.cn@Gmail.com * @create: 2020-09-07 */ public class ProduceConsumerVersion3 { private int i = 0; private final Object LOCK = new Object(); private volatile boolean isProduced = false; public void produce() { synchronized (LOCK) { while (isProduced) { try { //System.out.println(Thread.currentThread().getName()+"wait...."); LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "->" + (++i)); LOCK.notifyAll(); isProduced = true; } } public void consume() { synchronized (LOCK) { while (!isProduced) { try { //System.out.println(Thread.currentThread().getName()+"wait...."); LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "->" + i); LOCK.notifyAll(); isProduced = false; } } public static void main(String[] args) { ProduceConsumerVersion3 pc = new ProduceConsumerVersion3(); Stream.of("P1", "P2", "P3").forEach(n -> new Thread(new Runnable() { @Override public void run() { while (true) { pc.produce(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }, n).start()); Stream.of("C1", "C2", "C3", "C4").forEach(n -> new Thread(() -> { while (true) { pc.consume(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }, n).start()); } } ``` 運行效果如下: ![](https://img.kancloud.cn/f2/08/f20867c1bc65bbe48ba6954bbc590b0e_1154x456.gif)
                  <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>

                              哎呀哎呀视频在线观看