一 使用synchronized实现生产消费者模式
public class Demo {
public static void main(String[] args) {
AppleContainer container = new AppleContainer();
new Thread(new Producer(container),"AA").start();
new Thread(new Consumer(container),"BB").start();
}
}
// 生产者
class Producer implements Runnable {
private AppleContainer container;
public Producer(AppleContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("生产----------------------苹果:" + (i+1));
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
container.increace();
}
}
}
// 消费者
class Consumer implements Runnable {
private AppleContainer container;
public Consumer(AppleContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println(Thread.currentThread().getName()+"消费----------------------苹果:" + (i+1));
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
container.decreace();
}
}
}
class AppleContainer {
private int apple;
public synchronized void increace() {
if (apple == 5) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
apple++;
System.out.println("生产有苹果:"+apple);
notifyAll();
}
public synchronized void decreace() {
if (apple == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
apple--;
System.out.println("消费有苹果:"+apple);
notifyAll();
}
}
二 wait,notify,notifyAll 实现
public class Demo {
public static void main(String[] args) {
// 创建共享池
Container container = new Container();
new MyThread(container).start();
new MyThread(container).start();
new MyThread(container).start();
new MyThread1(container).start();
new MyThread1(container).start();
new MyThread1(container).start();
}
}
class MyThread extends Thread {
private Container container;
public MyThread(Container container) {
this.container = container;
}
@Override
public void run() {
container.get();
}
}
class MyThread1 extends Thread {
private Container container;
public MyThread1(Container container) {
this.container = container;
}
@Override
public void run() {
container.put();
}
}
class Container {
boolean flag = true;
public synchronized void put() {
while (true) {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "放入内容.......");
flag = false;
// 唤醒拿内容线程
notifyAll();
}
}
public synchronized void get() {
while (true) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "拿出内容.......");
flag = true;
notifyAll();
}
}
}
三 阻塞队列实现
/**
* 使用阻塞队列实现生产消费者模式
*/
public class Demo {
public static void main(String[] args) {
// 创建阻塞队列(先进先出)
BlockingQueue<Integer> proQueue = new LinkedBlockingQueue<>(4);
new Thread(new ProducerQueue(proQueue),"AA").start();
new Thread(new ConsumerQueue(proQueue),"BB").start();
}
}
class ProducerQueue implements Runnable {
private BlockingQueue<Integer> proQueue;
public ProducerQueue(BlockingQueue<Integer> proQueue) {
this.proQueue = proQueue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("生产了编号为:"+i);
try {
Thread.sleep(1000);
proQueue.put(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ConsumerQueue implements Runnable {
private BlockingQueue<Integer> proQueue;
public ConsumerQueue(BlockingQueue<Integer> proQueue) {
this.proQueue = proQueue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("消费了编号为:"+proQueue.take());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
四 Condition实现
public class demo {
public static void main(String[] args){
Basket b = new Basket();
Product p = new Product(b);
ConsumerCondition c = new ConsumerCondition(b);
ConsumerCondition c1 = new ConsumerCondition(b);
new Thread(p,"生产者1").start();
new Thread(c,"消费者1").start();
new Thread(c1,"消费者2").start();
}
}
// 生产者
class Product implements Runnable {
Basket basket;
public Product(Basket basket) {
this.basket = basket;
}
public void run() {
for (int i = 0; i < 10; i++) {
ManTou m = new ManTou(i);
basket.push(m);
System.out.println(Thread.currentThread().getName()+"生产了" + m);
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 消费者
class ConsumerCondition implements Runnable {
Basket basket;
public ConsumerCondition(Basket basket) {
this.basket = basket;
}
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
ManTou m = basket.pop();
System.out.println(Thread.currentThread().getName()+"消费了" + m);
}
}
}
// 装馒头的篮子
class Basket {
int max = 6;
LinkedList<ManTou> manTous = new LinkedList<ManTou>();
Lock lock = new ReentrantLock(); // 锁对象
Condition full = lock.newCondition(); // 用来监控篮子是否满的Condition实例
Condition empty = lock.newCondition(); // 用来监控篮子是否空的Condition实例
// 往篮子里面放馒头
public void push(ManTou m) {
lock.lock();
try {
while (max == manTous.size()) {
System.out.println("篮子是满的,待会儿再生产...");
full.await(); // wait
}
manTous.add(m);
empty.signalAll(); // notfiy
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
// 从篮子里面取馒头
public ManTou pop() {
ManTou m = null;
lock.lock();
try {
while (manTous.size() == 0) {
System.out.println("篮子是空的,待会儿再吃...");
empty.await();
}
m = manTous.removeFirst();
full.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return m;
}
}
// 馒头
class ManTou {
int id;
public ManTou(int id) {
this.id = id;
}
@Override
public String toString() {
return "ManTou" + id;
}
}