Java多线程之生产者消费者模型案例

案例一

//写一个模拟汽车生产流水线的程序
//1. 要求生产 30 辆车,其有生产序号, 名称
//在生产前,检查前面生产的产品是否已被取出,如未取出,则不能生产
//在设置生产序号和名称之前休眠 20ms
//生产好的汽车放到流水线上
//2. 装运工把在流水线生产好的汽车取出,输出汽车序号和名称信息
//要求输出的生产序号和名称相一致
public class MainClass {
    public static void main(String[] args) {
        Car car = new Car();
        CarProduction carProduction = new CarProduction(car);
        CarConsumption carConsumption = new CarConsumption(car);
        new Thread(carConsumption).start();
        new Thread(carProduction).start();
    }
}

class CarProduction implements Runnable {

    private Car car;

    public CarProduction() {
    }

    public CarProduction(Car car) {
        this.car = car;
    }

    @Override
    public synchronized void run() {
        for (int i = 1; i <= 30; i++) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i % 2 == 0) {
                car.set("汽车" + i, i);
            } else {
                car.set("卡车" + i, i);
            }
        }
    }
}

class CarConsumption implements Runnable {
    private Car car;

    public CarConsumption() {
    }

    public CarConsumption(Car car) {
        this.car = car;
    }

    @Override
    public synchronized void run() {
        for (int i = 1; i <= 100; i++) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            car.get();
        }
    }
}

class Car {
    private String name;
    private int id;

    private boolean flag = false;

    public Car() {
    }

    public Car(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public synchronized void get() {
        if (!flag) {
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(this.name + "->" + this.id);

        flag = false;
        super.notify();
    }

    public synchronized void set(String name, int id) {
        if (flag) {
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.name = name;

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        this.id = id;

        flag = true;
        super.notify();
    }
}

在这里插入图片描述

案例二

三个生产商与三个消费者共用一个仓库,生产商会不断的生产商品,消费者同样也在不断的消费,但当仓库满仓时,生产商停止生产,但消费者同时还在不断的消费,当仓库有空闲仓位时,生产商继续生产。同样消费者也是,当仓库没有商品时,消费者等生产商生产出商品后,才继续消费。

public class MainClass {
    public static void main(String[] args) {
        Store store = new Store();
        new Thread(new Consumer("张三", store)).start();
        new Thread(new Consumer("李四", store)).start();
        new Thread(new Consumer("王五", store)).start();
        new Thread(new Producer("第一工厂", store)).start();
        new Thread(new Producer("第二工厂", store)).start();
    }
}

class Store {
    // 仓位最多为10个
    private final int MAX = 10;
    private int num = 0;

    public synchronized void produce(Producer producer) throws InterruptedException {
        while (num == MAX) {
            System.out.println("仓库已满 ,请等待空位");
            wait();
        }
        ++num;
        System.out.println(producer.name + " 生产了后,仓库现存 :" + num + "个产品");
        notify();// 唤醒其它进程
    }

    public synchronized void consumer(Consumer consumer) throws InterruptedException {
        while (num == 0) {
            System.out.println("仓库空仓,请等待产品入库");
            wait();
        }
        --num;
        System.out.println(consumer.name + " 消费后,还剩 :" + num + "个产品");
        notify();// 唤醒其它进程
    }
}

class Producer implements Runnable {
    String name;
    Store store;

    public Producer(String name, Store store) {
        this.name = name;
        this.store = store;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep((long) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                store.produce(this);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Consumer implements Runnable {
    String name;
    Store store;

    public Consumer(String name, Store store) {
        this.name = name;
        this.store = store;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep((long) (Math.random() % 30 * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                store.consumer(this);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

在这里插入图片描述

案例3

两人从同一银行账户取钱

class UserGetMoney implements Runnable{
    private static int sum = 2000;
    private int money = 0;

    public UserGetMoney(int money) {
        this.money = money;
    }

    public synchronized void take(int k) {
        int temp = sum;
        temp -= k;
        try {
            Thread.sleep((int) (100 * Math.random()));
        } catch (InterruptedException e) {
        }
        sum = temp;
        System.out.println(Thread.currentThread() + "sum = " + sum);
    }

    public void run() {
        for (int i = 1; i <= 4; i++) {
            take(money);
        }
    }
}


public class BankTest {
    public static void main(String[] args) {
        UserGetMoney u = new UserGetMoney(100);
        new Thread(u).start();
        new Thread(u).start();
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿炳的旅程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值