使用synchronized实现生产者消费者模型案例

本文介绍了一个使用synchronized关键字实现的生产者消费者模型案例。通过Java代码,详细展示了如何在多线程环境下,利用同步机制控制商品的生产和消费过程,避免资源竞争,确保线程安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生产者

使用synchronized实现生产者消费者模型案例
生产者代码:

public class Produce implements Runnable {
    Product product;
    public Produce(Product product){
        this.product=product;

    }
    @Override
    public void run() {

        while (true){
            product.push();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

消费者代码

public class Comsuer implements Runnable {
    Product product;
    public Comsuer(Product product){
        this.product = product;
    }
    @Override
    public void run() {
        while (true){
            product.take();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

商品代码

public class Product {
    final int MAX_NUMBER = 10;
    private int max;

    public synchronized void push() {
        while (max >= MAX_NUMBER) {
            try {
                System.out.println(Thread.currentThread().getName() + "生产过剩,当前:" + max);
                this.wait();
            } catch (Exception e) {

            }
        }
        this.max++;
        System.out.println(Thread.currentThread().getName() + "生产+1,当前:" + max);
        notifyAll();
    }

    public synchronized void take() {
        while (max <= 0) {
            try {
                System.out.println(Thread.currentThread().getName() + "消费已完,当前:" + max);
                this.wait();
            } catch (Exception e) {
            }
        }
        this.max--;
        System.out.println(Thread.currentThread().getName() + "消费-1,当前:" + max);
        notifyAll();
    }
}

Main方法

public class Main {



    public static void main(String[] args) {
        System.out.println("Hello World!");
        Product product = new Product();
        Comsuer comsuer = new Comsuer(product);
        Produce produce = new Produce(product);
        new Thread(produce).start();
        new Thread(produce).start();
        new Thread(produce).start();
        new Thread(produce).start();
        new Thread(produce).start();
        new Thread(produce).start();
        new Thread(produce).start();
        new Thread(comsuer).start();
        new Thread(comsuer).start();
        new Thread(comsuer).start();

    }
}

结果:

Hello World!
Thread-0生产+1,当前:1
Thread-1生产+1,当前:2
Thread-2生产+1,当前:3
Thread-3生产+1,当前:4
Thread-7消费-1,当前:3
Thread-5生产+1,当前:4
Thread-6生产+1,当前:5
Thread-4生产+1,当前:6
Thread-8消费-1,当前:5
Thread-9消费-1,当前:4
Thread-5生产+1,当前:5
Thread-3生产+1,当前:6
Thread-0生产+1,当前:7
Thread-2生产+1,当前:8
Thread-1生产+1,当前:9
Thread-7消费-1,当前:8

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值