package test;
/**
* Synchronized 版本解决生产者消费者
* wait() / notify()方法
*/
import java.util.LinkedList;
import java.util.Queue;
public class ProducerAndConsumerForSynchronized {
// 1. 先定义最大生产长度
private final int MAX_SIZE = 10;
// 2.定义储存队列
private Queue
// 3.定义生产者
class Producer extends Thread {
@Override
public void run() {
producer();
}
private void producer() {
while (true) {
synchronized (storeList) {
while (storeList.size() == MAX_SIZE) {
storeList.notify();
System.out.println("当前库存数量达到最大值:" + MAX_SIZE + "条");
try {
// 暂停生产
storeList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.add(new Object());
storeList.notify();
System.out.println("生产者产量增加一条,库存" + storeList.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// 4.定义消費者
class Consumer extends Thread {
@Override
public void run() {
consumer();
}
private void consumer() {
while (true) {
synchronized (storeList) {
while (storeList.size() == 0) {
storeList.notify();
System.out.println("当前库存为空");
try {
// 暂停消费
storeList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.poll();
storeList.notify();
System.out.println("消费者消费一条产量,当前库存为" + storeList.size());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumerForSynchronized pc = new ProducerAndConsumerForSynchronized();
Producer producer = pc.new Producer();
Consumer consumer = pc.new Consumer();
producer.start();
consumer.start();
}
}
package test;
/**
* Lock 版本解决生产者消费者
* await() / signal()方法
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerAndConsumerForLock {
// 1. 先定义最大生产长度
private final int MAX_SIZE = 10;
// 2.定义储存队列
private Queue
private final Lock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
class Producer extends Thread {
@Override
public void run() {
producer();
}
private void producer() {
while (true) {
lock.lock();
try {
while (storeList.size() == MAX_SIZE) {
System.out.println("当前库存满");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.add(1);
condition.signal();
System.out.println("生产者产量增加一条,库存" + storeList.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
}
}
class Consumer extends Thread {
@Override
public void run() {
consumer();
}
private void consumer() {
while (true) {
lock.lock();
try {
while (storeList.size() == 0) {
System.out.println("当前库存为空");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.poll();
condition.signal();
System.out.println("消费者消费一条任务,当前队列长度为" + storeList.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumerForLock pc = new ProducerAndConsumerForLock();
Producer producer = pc.new Producer();
Consumer consumer = pc.new Consumer();
producer.start();
consumer.start();
}
}
package test;
/**
* BlockingQueue阻塞队列版本解决生产者消费者
* put() / take() 方法
*/
import java.util.concurrent.LinkedBlockingQueue;
public class ProducerAndConsumerForBlockingQueue {
// 定义储存队列
private LinkedBlockingQueue
class Producer extends Thread {
@Override
public void run() {
produce();
}
public void produce() {
while (true) {
try {
storeList.put(new Object());
System.out.println("生产者生产了一个商品 ,库存:" + storeList.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
@Override
public void run() {
consume();
}
public void consume() {
while (true) {
try {
storeList.take();
System.out.println("消费者消费了一个商品 ,库存:" + storeList.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumerForBlockingQueue pc = new ProducerAndConsumerForBlockingQueue();
Producer producer = pc.new Producer();
Consumer consumer = pc.new Consumer();
producer.start();
consumer.start();
}
}