wait、notify 实现方式
public class ProducerAndConsumer {
public static void main(String[] args) {
GoodsFactory goodsFactory = new GoodsFactory();
new Producer(goodsFactory).start();
new Consumer(goodsFactory).start();
}
}
class Producer extends Thread {
private GoodsFactory goodsFactory;
public Producer(GoodsFactory goodsFactory) {
this.goodsFactory = goodsFactory;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(new Random().nextInt(1000));
goodsFactory.produce();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer extends Thread {
private GoodsFactory goodsFactory;
public Consumer(GoodsFactory goodsFactory) {
this.goodsFactory = goodsFactory;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(new Random().nextInt(1000));
goodsFactory.consume();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class GoodsFactory {
private List<Integer> goods = new ArrayList<>();
private AtomicInteger atomicInteger = new AtomicInteger();
public synchronized void produce() throws InterruptedException {
if (goods.size() >= 5) {
System.out.println("商品已满,等待被消费!");
wait();
} else {
goods.add(atomicInteger.incrementAndGet());
System.out.println("生产了一个商品,当前共有: " + goods.size() + "个");
notifyAll();
}
}
public synchronized void consume() throws InterruptedException {
if (goods.size() <= 0) {
System.out.println("商品已空,等待生产!");
wait();
} else {
goods.remove(0);
System.out.println("消费了一个商品,当前共有: " + goods.size() + "个");
notifyAll();
}
}
}
Lock、Condition 实现方式
public class ProducerAndConsumer {
public static void main(String[] args) {
GoodsFactory goodsFactory = new GoodsFactory();
new Producer(goodsFactory).start();
new Consumer(goodsFactory).start();
}
}
class Producer extends Thread {
private GoodsFactory goodsFactory;
public Producer(GoodsFactory goodsFactory) {
this.goodsFactory = goodsFactory;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(new Random().nextInt(1000));
goodsFactory.produce();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer extends Thread {
private GoodsFactory goodsFactory;
public Consumer(GoodsFactory goodsFactory) {
this.goodsFactory = goodsFactory;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(new Random().nextInt(1000));
goodsFactory.consume();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class GoodsFactory {
private List<Integer> goods = new ArrayList<>();
private AtomicInteger atomicInteger = new AtomicInteger();
Lock lock = new ReentrantLock();
Condition notFull = lock.newCondition();
Condition notEmpty = lock.newCondition();
public void produce() throws InterruptedException {
try {
lock.lock();
if (goods.size() >= 5) {
System.out.println("商品已满,等待被消费!");
notFull.await();
} else {
goods.add(atomicInteger.incrementAndGet());
System.out.println("生产了一个商品,当前共有: " + goods.size() + "个");
notEmpty.signal();
}
} finally {
lock.unlock();
}
}
public void consume() throws InterruptedException {
try {
lock.lock();
if (goods.size() <= 0) {
System.out.println("商品已空,等待生产!");
notEmpty.await();
} else {
goods.remove(0);
System.out.println("消费了一个商品,当前共有: " + goods.size() + "个");
notFull.signal();
}
}finally {
lock.unlock();
}
}
}
LinkedBlockingDeque 实现方式
public class ProducerAndConsumer {
public static void main(String[] args) {
GoodsFactory goodsFactory = new GoodsFactory();
new Producer(goodsFactory).start();
new Consumer(goodsFactory).start();
}
}
class Producer extends Thread {
private GoodsFactory goodsFactory;
public Producer(GoodsFactory goodsFactory) {
this.goodsFactory = goodsFactory;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(new Random().nextInt(1000));
goodsFactory.produce();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer extends Thread {
private GoodsFactory goodsFactory;
public Consumer(GoodsFactory goodsFactory) {
this.goodsFactory = goodsFactory;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(new Random().nextInt(1000));
goodsFactory.consume();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class GoodsFactory {
private AtomicInteger atomicInteger = new AtomicInteger();
private BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque(5);
public void produce() throws InterruptedException {
blockingDeque.put(atomicInteger.incrementAndGet());
System.out.println("生产了一个商品,当前共有: " + blockingDeque.size() + "个");
}
public void consume() throws InterruptedException {
blockingDeque.take();
System.out.println("消费了一个商品,当前共有: " + blockingDeque.size() + "个");
}
}