多线程09.生产者消费者网络版
package com.wangwenjun.concurrency.chapter9; import java.util.stream.Stream; public class ProduceConsumerVersion3 { private int i = 0; final private Object LOCK = new Object(); private volatile boolean isProduced = false; public void produce() { synchronized (LOCK) { while (isProduced) { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } i++; System.out.println("P->" + i); LOCK.notifyAll(); isProduced = true; } } public void consume() { synchronized (LOCK) { while (!isProduced) { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("C->" + i); LOCK.notifyAll(); isProduced = false; } } public static void main(String[] args) { ProduceConsumerVersion3 pc = new ProduceConsumerVersion3(); Stream.of("P1", "P2", "P3").forEach(n -> new Thread(n) { @Override public void run() { while (true) { pc.produce(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start() ); Stream.of("C1", "C2", "C3", "C4").forEach(n -> new Thread(n) { @Override public void run() { while (true) { pc.consume(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start() ); } }
---------------------------------------------------------------------------
国之殇,未敢忘!
南京大屠杀!
731部队!
(有关书籍《恶魔的饱食》)以及核污染水排海等一系列全无人性的操作,购买他们的食品和为它们提供帮助只会更加变本加厉的害你,呼吁大家不要购买日本相关产品
昭昭前事,惕惕后人
吾辈当自强,方使国不受他人之侮!
---------------------------------------------------------------------------
作者:三号小玩家
出处:https://www.cnblogs.com/q1359720840/
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 版权信息