采用synchronized锁以及wait notify方法实现
1.首先创建消费的产品对象
class Person{
private int foodNum = 0;
private Object synObj = new Object();
private final int MAX_NUM = 5;
public void produce() throws InterruptedException{
synchronized (synObj){
while (foodNum == 5){
System.out.println("box is full,size = " + foodNum);
synObj.wait();
}
foodNum++;
System.out.println("produce success foodNum = " + foodNum);
synObj.notifyAll();
}
}
public void consume() throws InterruptedException{
synchronized (synObj){
while (foodNum == 0){
System.out.println("box is empty,size = " + foodNum);
synObj.wait();
}
foodNum--;
System.out.println("consume success foodNum = " + foodNum);
synObj.notifyAll();
}
}
}
2.定义生产者消费者方法
//生产者
class Producer implements Runnable{
private Person person;
private String producerName;
public Producer(String producerName, Person person){
this.producerName = producerName;
this.person = person;
}
@Override
public void run(){
while (true){
try{
person.produce();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
//消费者
class Consumer implements Runnable{
private Person person;
private String consumerName;
public Consumer(String consumerName, Person person){
this.consumerName = consumerName;
this.person = person;
}
@Override
public void run(){
try{
person.consume();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
3.创建方法调用生产者和消费者
public class WaitAndNotify{
public static void main(String[] args) throws IOException{
Person person = new Person();
new Thread(new Consumer("消费者一", person)).start();
new Thread(new Consumer("消费者二", person)).start();
new Thread(new Consumer("消费者三", person)).start();
new Thread(new Producer("生产者一", person)).start();
new Thread(new Producer("生产者一", person)).start();
new Thread(new Producer("生产者一", person)).start();
}
}