题目:生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品,店员一次只能持有固定数量的产品(比如:20),如果生产者试图生产更多的产品,店员会叫生产者停一下,如果店中有空位放产品了再通知生产者继续生产;如果店中没有产品了,店员会告诉消费者等一下,如果店中有产品了再通知消费者来取走产品。
分析:
1.是否是多线程的问题:是
2.是否有共享数据:是,店员手中的商品数
3.如何解决线程安全问题:以下代码使用同步方法
4.是否涉及到线程通信:是
在生产者生产到20个商品后,就必须强制停止生产,所以使用wait()使线程释放同步监视器,在消费者消费商品后使用notify()唤醒生产线程。在商品数到达0时,就要使消费者强制停止消费,使用wait(),在生产者生产出商品后,使用notify()唤醒消费线程。
以下代码由于类名重复,所以店员(Clerk)类改为Production类,生产者是shengchan类,消费者是xiaofei类。
先贴上正确代码:
package com.cuit.exer;
/**
*
* 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处
* 取走产品,店员一次只能持有固定数量的产品(比如:20),如果生产者试图
* 生产更多的产品,店员会叫生产者停一下,如果店中有空位放产品了再通
* 知生产者继续生产;如果店中没有产品了,店员会告诉消费者等一下,如
* 果店中有产品了再通知消费者来取走产品。
*
* @author lichishui
* @create 2022-04-16 12:01
*/
public class AccountTest1 {
public static void main(String[] args) {
shengchan s = new shengchan();
xiaofei x = new xiaofei();
Thread st1 = new Thread(s);
Thread xt1 = new Thread(x);
st1.setName("生产者1 ");
xt1.setName("消费者1 ");
st1.start();
xt1.start();
}
}
class Production{
private static int count = 0;
private Production(){
}
private static Production p = new Production();
public static Production getProduction(){
return p;
}
public synchronized void add(){
if(count < 20){
count++;
System.out.println(Thread.currentThread().getName() + "生产第" + count + "个产品");
notify();
}else{
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public synchronized void des(){
if(count > 0){
System.out.println(Thread.currentThread().getName() + "消耗第" + count + "个产品");
count--;
notify();
}else {
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
class shengchan implements Runnable{
@Override
public void run() {
while(true){
Production.getProduction().add();
}
}
}
class xiaofei implements Runnable{
@Override
public void run() {
while(true){
Production.getProduction().des();
}
}
}
重点在于Producation类,必须使用单例模式来实现,上述代码是使用了饿汉式来实现。因为在shenchan类和xiaofei类中,声明了Production p,共享数据是存在Production中的,所以必须是操作同一个对象才行。
若要实现继承Thread类完成,推荐尚硅谷康师傅java 生产者消费者例题。