JAVA------ Producer Consumer

Overview of the producer-consumer model

In order to reflect the waiting and wake-up in the process of production and consumption, Java provides several methods for us to use. These methods are the waiting and wake-up methods of the Object class in the Object class:

  • void wait(): Cause the current thread to wait until another thread calls the notify() method or notifyAll() method of the object

  • void notify(): wake up a single thread that is waiting for the object monitor

  • void notifyAll(): wake up all threads that are waiting for the object monitor


Case:

The producer will produce milk and send it to the milk box, and the consumer can take the milk from the milk box, but the producer must wait for the consumer to take the milk before putting it in, and the consumer must wait for the producer to put the milk in. Take it away and implement it with a program.

Ideas:

The classes included in the producer consumer case:

  1. Milk box class (Box): define a member variable, representing the xth bottle of milk, providing operations for storing milk and obtaining milk
  2. Producer class (Producer): implement the Runnable interface, rewrite the run() method, and call the operation of storing milk
  3. Consumer class (Customer): implement the Runnable interface, rewrite the run() method, and call the operation of obtaining milk
  4. Test class (BoxDemo): There is a main method in it. The code steps in the main method are as follows: ①Create
    a milk box object, which is a shared data area.
    ②Create a producer object and pass the milk box object as a constructor parameter, because in this class To call the operation of storing milk
    ③ create a consumer object, pass the milk box object as a parameter of the constructor, because in this class you need to call the operation to get milk
    ④ Create two thread objects, take the producer object and the consumer object as Construction method parameter transfer
    ⑤Start thread

First create the Box class:

In the Box class, wait() and notifyAll() operations are used to realize the process of waiting for consumption and waiting for production. Look at the code specifically.

package 生产者消费者;

public class Box {
    
    
	//定义一个成员变量,表示第x瓶奶
	private int milk;
	//定义一个成员变量,表示奶箱状态
	private boolean state=false; 
	
	//提供存储牛奶和获取牛奶的操作
	public synchronized void put(int milk) {
    
    
		//如果有牛奶,等待消费
		if(state) {
    
    
			try {
    
    
				wait();
			} catch ( InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
		//如果没有,就生产牛奶
		this.milk=milk;
		System.out.println("送奶工将第"+this.milk+"瓶奶放入奶箱");
		
		state=true;
		
		//唤醒其他等待的线程
		notifyAll();
	}
	
	public synchronized void get() {
    
    
		//如果没有牛奶,就等待生产
		if(!state) {
    
    
			try {
    
    
				wait();
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
		
		//如果有牛奶,就消费
		System.out.println("用户拿到第"+this.milk+"瓶奶");
		
		state=false;
		//唤醒其他等待的线程
		notifyAll();
	}

}

Producer class:

package 生产者消费者;

public class Productor implements Runnable{
    
    
	private Box b;
	
	public Productor(Box b) {
    
    
		this.b=b;
	}
	
	@Override
	public void run() {
    
    
		for(int i=1;i<=5;i++) {
    
    
			b.put(i);
		}
	}

}

Consumer category:

package 生产者消费者;

public class Customer implements Runnable{
    
    
	private Box b;
	
	public Customer(Box b) {
    
    
		this.b=b;
	}
	
	@Override
	public void run() {
    
    
		while(true) {
    
    
			b.get();
		}
	}

}

main class:

package 生产者消费者;

public class BoxDemo {
    
    
	public static void main(String[] args) {
    
    
		//创建奶箱对象,这是共享数据区域
		Box b=new Box();
		
		//创建生产者对象,把奶箱对象作为构造方法参数传递, 因为在这个类中要调用存储牛奶的操作
		Productor p=new Productor(b);
		
		//创建消费者对象,把奶箱对象作为构造方法参数传递,因为在这个类中要调用获取牛奶的操作
		Customer c=new Customer(b);
		
		//创建2个线程对象,分别把生产者对象和消费者对象作为构造方法参数传递
		Thread t1=new Thread(p);
		Thread t2=new Thread(c);
		
		//启动线程
		t1.start();
		t2.start();
	}

}

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113762240