这是一个线程同步问题,生产者消费者共享一个资源,并且生产者消费者之间相互依赖,互为条件:
对于生产者,没有生产产品之前,需通知消费者等待,而生产产品之后需要马上通知消费者消费;
对于消费者,在消费之后,需要通知生产者已经结束消费,需要生产新的产品提供消费;
在生产者消费者问题中,仅有synchronized是不够的;
synchronized可阻止并发更新同一个共享资源,实现了同步;
synchronized不能用来实现不同线程之间的消息传递(通信);
解决方式一:管程法(并发协作模型)
生产者:负责生产数据模块
消费者:负责处理数据模块
缓冲区:生产者将生产好的数据放入缓冲区,消费者从缓冲区拿出数据
实例:
package com.keji.oop;
//解决生产者消费者之间的问题,利用缓冲区解决--管程法
public class PC {
public static void main(String[] args) {
Buffer container = new Buffer();
new Producer(container).start();
new Consumer(container).start();
}
}
//生产者
class Producer extends Thread{
Buffer container;
public Producer(Buffer container) {
this.container = container;
}
//生产
@Override
public void run() {
for (int i = 0; i < 100; i++) {
container.push(new Product(i));
System.out.println("生产了"+i+"个产品");
}
}
}
//消费者
class Consumer extends Thread{
Buffer container;
public Consumer(Buffer container) {
this.container = container;
}
//消费
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了"+container.pop().id+"个产品");
}
}
}
//产品
class Product{
int id;
public Product(int id) {
this.id = id;
}
}
//缓冲区
class Buffer{
//需要一个容器
Product[] products = new Product[10];
//容器计数器
int count = 0;
//生产者丢入产品
public synchronized void push(Product product){
//如果容器满了,需要等待消费者消费
if (count== products.length){
//通知消费者来消费,生产者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果容器未满,需要丢入产品
products[count] = product;
count++;
//通知消费者可以消费了
this.notifyAll();
}
//消费者消费产品
public synchronized Product pop(){
//判断能否消费
if (count==0){
//消费者等待生产者生产
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果可以消费
count--;
Product product = products[count];
//通知生产者已经消费,让生产者生产
this.notifyAll();
return product;
}
}
结果:
通过缓冲区解决了生产者消费者的问题!
解决方式二:信号灯法(并发协作模型)
类似红灯停,绿灯行,通过使用标志位方式,来判断线程的监听、唤醒、等待等;
实例:
package com.keji.oop;
//测试生产者消费者方法2--信号灯法:标志位解决
public class PC2 {
public static void main(String[] args) {
Program program = new Program();
new Actor(program).start();
new Audience(program).start();
}
}
//生产者--演员
class Actor extends Thread{
Program program;
public Actor(Program program){
this.program = program;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2==0){
this.program.act("节目");
}else {
this.program.act("广告");
}
}
}
}
//消费者--观众
class Audience extends Thread {
Program program;
public Audience(Program program){
this.program = program;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
program.watch();
}
}
}
//产品--节目
class Program{
//演员表演,观众等待 T
//演员等待,观众观看 F
String voice;//表演的节目
boolean flag = true;//演员
//表演方法
public synchronized void act(String voice){
if (!flag){
try {
this.wait();//演员等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了"+voice);
//通知观众观看
this.notifyAll();//通知唤醒
this.voice = voice;
this.flag = !this.flag;
}
//观看方法
public synchronized void watch(){
if (flag){
try {
this.wait();//观众等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众观看了"+voice);
//观看完后,通知演员表演
this.notifyAll();
this.flag = !this.flag;
}
}
结果:
通过使用标志位解决了生产者消费者之间的问题!