JAVA 多线程(一)简单生产者和消费者

本文介绍了一个使用Java实现的简单仓库类,通过synchronized关键字和wait/notify机制实现生产者消费者模型。生产者和消费者线程通过Depot类进行产品生产和消费,探讨了线程同步和阻塞操作在并发控制中的应用。
摘要由CSDN通过智能技术生成
//仓库代码
public class Depot {

    private int capacity;
    private int size=0;

    public Depot(int c){
        this.capacity=c;
    }

    public synchronized void product(int count) throws InterruptedException {
        while(count>0){
            if(size>capacity){
                wait();
            }
            //真实的生产数量
            int realCount=(capacity-size)>=count?count:(capacity-size);
            System.out.print(Thread.currentThread().getName()+"--本次想要生产:"+count+",本次实际生产:"+realCount);
            //下次生产数量
            count=count-realCount;
            //仓库剩余
            size+=realCount;
            System.out.println(",下次想要生产:"+count+",仓库真实容量:"+size);
            notifyAll();
        }
    }

    public synchronized void comsume(int count) throws InterruptedException {
        while (count>0){
            if(size<=0){
                wait();
            }
            
            //真实消费数量
            int realCount=(size>count)?count:size;
            System.out.print(Thread.currentThread().getName()+"--本次想要消费:"+count+",本次真实消费:"+realCount);
            //下次消费
            count=count-realCount;
            //仓库剩余
            size-=realCount;
            System.out.println("下次想要消费:"+count+",仓库剩余:"+size);
            notify();
        }
    }



}

 

//生产者
public class Product {

    Depot depot;

    public Product(Depot depot){
        this.depot=depot;
    }

    public void produce(final int count){
        new Thread() {
            public void  run(){
                try {
                    depot.product(count);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
//消费者
public class Comsumer {

    Depot depot;

    public Comsumer(Depot depot){
        this.depot=depot;
    }

    public void  comsumers(final  int count){
        new Thread(new Runnable() {
            @Override
            public void run() {
                    try {
                        depot.comsume(count);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        }){
        }.start();

    }

}

 

 

//测试
public class TestThread {

    public static void main(String[] args) {

        Depot depot=new Depot(100);

        Product product=new Product(depot);
        Comsumer comsumer=new Comsumer(depot);

        product.produce(20);
        product.produce(30);
        product.produce(20);

        comsumer.comsumers(40);
        comsumer.comsumers(20);

        product.produce(30);

        comsumer.comsumers(20);
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值