生产者消费者问题--synchronized

# 代码

public class App {

    public static void main(String[] args) {

        Depot depot = new Depot(100);
        Producer producer = new Producer(depot);
        Consumer consumer = new Consumer(depot);

        producer.produce(60);
        consumer.consume(100);
        producer.produce(90);
        consumer.consume(40);

    }
}

class Depot {
    // 仓库最大容量
    private int capacity;
    // 仓库目前容量
    private int size;

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

    public synchronized void produce(int val) {
        try {
            int surplus = val;
            while (surplus > 0) {
                while (size >= capacity) {
                    wait();
                }
                int incre = (size + surplus) > capacity ? (capacity - size) : surplus;
                size += incre;
                surplus -= incre;
                System.out.printf("%s plan to produce (%d), actually produce (%d), depot size (%d) \n",
                        Thread.currentThread().getName(), val, incre, size);
                notify();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void consume(int val) {
        try {
            int surplus = val;
            while (surplus > 0) {
                while (size <= 0) {
                    wait();
                }
                int desc = (size < surplus) ? size : surplus;
                size -= desc;
                surplus -= desc;
                System.out.printf("%s plan to consume (%d), actutally consume (%d), depot size (%d) \n",
                        Thread.currentThread().getName(), val, desc, size);
                notify();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Producer {
    private Depot depot;

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

    public void produce(final int val) {
        new Thread() {
            public void run() {
                depot.produce(val);
            }
        }.start();
    }
}

class Consumer {
    private Depot depot;

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

    public void consume(final int val) {
        new Thread() {
            public void run() {
                depot.consume(val);
            }
        }.start();
    }
}

# 输出:

Thread-0 plan to produce (60), actually produce (60), depot size (60)
Thread-3 plan to consume (40), actutally consume (40), depot size (20)
Thread-2 plan to produce (90), actually produce (80), depot size (100)
Thread-1 plan to consume (100), actutally consume (100), depot size (0)
Thread-2 plan to produce (90), actually produce (10), depot size (10)

# 有四个线程参与了这个过程,两个生产者,两个消费者

 

转载于:https://www.cnblogs.com/lwmp/p/11512636.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值