生产者-消费者-管程法(java代码示例)

 首先要清楚一共4几个对象

 1,生产者2.消费者3.产品4.容器

 容器必须要有一个容量,通俗来讲,就是你的容器可以装多少产品,这是有限的。

生产者可以生产产品然后放到容器中,消费者可以消费产品,(把产品从容器中拿出来) 

如果容器没有产品,消费者拿不到,那么通知生产者去生产,生产者生产了,又去通知消费者去拿。

如果容器已经满了,生产者会暂停生产,然后通知消费者去容器里拿产品。

话不多说,上代码


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
/**
 * @ProductName:
 * @ProjectName: 
 * @Package: 
 * @Description:
 * @Author:四叶总会来的
 * @Date: 
 * @UpdateRemark:
 * @Version: 1.0
 * <p>
 */
@Slf4j
public class ProduceAndConsumer {

    public static void main(String[] args) {
        Container container=new Container(new Product[10]);
        Consumer consumer=new Consumer(container);
        Productor productor=new Productor(container);
        consumer.start();
        productor.start();
    }
}

/**
 * 生产者
 */
class Productor extends Thread {

    //生产产品
    /**
     *
     */
    Container container;

    /**
     *
     */
    public Productor(Container container) {
        this.container = container;
    }

    @SneakyThrows
    @Override
    public void run() {
        while (true) {

                container.push(new Product("q","1"));



        }
    }
}

/**
 * 消费者
 */
class Consumer extends Thread {
//消费产品
    /**
     *
     */
    Container container;

    /**
     *
     */
    public Consumer(Container container) {
        this.container = container;
    }

    @SneakyThrows
    @Override
    public void run() {
        while (true) {

                container.pob();


//             Thread.sleep(100);
        }
    }
}

/**
 * 产品
 */
class Product {
    /**
     *
     */
    private String name;
    /**
     *
     */
    private String num;

    public Product(String name, String num) {
        this.name = name;
        this.num = num;

    }
}

/**
 * 容器
 */
@Slf4j
class Container {
    public Product[] getProduct() {
        return product;
    }

    public void setProduct(Product[] product) {
        this.product = product;
    }

    /**
     *
     */
    private Product[] product;
    /**
     *
     */
    private Integer count;

    public Container(Product[] product) {
        this.product = product;
        this.count =-1;
    }

    /**
     * v
     *
     * @param p
     */
    public synchronized void push(Product p) throws InterruptedException {
        if (count <product.length-1) {
            //容器没有满,可以放东西
            count++;
            product[count] = p;
            log.info("位置" + count + "放入了一个产品");

            Thread.sleep(100);
            this.notify();
        } else {
            log.info("等待---->生产失败,容器已满"+count);
            this.wait();
//            Thread.sleep(1000);
        }
    }

    public synchronized void pob() throws InterruptedException {
        if (count < product.length && count >=0) {
            //容器有产品,可以消费
           log.info("消费了位置为" + count + "的产品");
            count--;
            this.notify();
            Thread.sleep(100);
        } else {
            log.info("等待---->消费失败,数量为" + count);
            this.wait();

        }
    }

}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值