java4.生产者消费者:有一个生产者对象,还有一个消费者对象生产者用来向篮子对象中
答案:2 悬赏:0 手机版
解决时间 2021-11-25 18:37
- 提问者网友:無理詩人
- 2021-11-24 23:45
java4.生产者消费者:有一个生产者对象,还有一个消费者对象生产者用来向篮子对象中
最佳答案
- 五星知识达人网友:不想翻身的咸鱼
- 2021-11-25 00:30
import java.util.LinkedList; public class TestProductConm { public static class Storage { // 仓库存储量 private final int MAX_SIZE = 一00; // 仓库存储载体 private LinkedList list = new LinkedList(); // 产num产品 public void produce(int num) { // 同步代码段 synchronized (list) { // 仓库剩余容量足 while (list.size() + num > MAX_SIZE) { System.out.println("【要产产品数量】:" + num + "/t【库存量】:" + list.size() + "/t暂能执行产任务!"); try { // 由于条件满足产阻塞 list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 产条件满足情况产num产品 for (int i = 一; i <= num; ++i) { list.add(new Object()); } System.out.println("【已经产产品数】:" + num + "/t【现仓储量】:" + list.size()); list.notifyAll(); } } // 消费num产品 public void consume(int num) { // 同步代码段 synchronized (list) { // 仓库存储量足 while (list.size() < num) { System.out.println("【要消费产品数量】:" + num + "/t【库存量】:" + list.size() + "/t暂能执行产任务!"); try { // 由于条件满足消费阻塞 list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 消费条件满足情况消费num产品 for (int i = 一; i <= num; ++i) { list.remove(); } System.out.println("【已经消费产品数】:" + num + "/t【现仓储量】:" + list.size()); list.notifyAll(); } } // get/set public LinkedList getList() { return list; } public void setList(LinkedList list) { this.list = list; } public int getMAX_SIZE() { return MAX_SIZE; } } public static class Producer extends Thread { // 每产产品数量 private int num; // 所放置仓库 private Storage storage; // 构造函数设置仓库 public Producer(Storage storage) { this.storage = storage; } // 线程run函数 public void run() { produce(num); } // 调用仓库Storage产函数 public void produce(int num) { storage.produce(num); } // get/set public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Storage getStorage() { return storage; } public void setStorage(Storage storage) { this.storage = storage; } } public static class Consumer extends Thread { // 每消费产品数量 private int num; // 所放置仓库 private Storage storage; // 构造函数设置仓库 public Consumer(Storage storage) { this.storage = storage; } // 线程run函数 public void run() { consume(num); } // 调用仓库Storage产函数 public void consume(int num) { storage.consume(num); } // get/set public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Storage getStorage() { return storage; } public void setStorage(Storage storage) { this.storage = storage; } } public static void main(String[] args) { // 仓库象 Storage storage = new Storage(); // 产者象 Producer p一 = new Producer(storage); Producer p二 = new Producer(storage); Producer p三 = new Producer(storage); Producer p四 = new Producer(storage); Producer p5 = new Producer(storage); Producer p陆 = new Producer(storage); Producer p漆 = new Producer(storage); // 消费者象 Consumer c一 = new Consumer(storage); Consumer c二 = new Consumer(storage); Consumer c三 = new Consumer(storage); // 设置产者产品产数量 p一.setNum(一0); p二.setNum(一0); p三.setNum(一0); p四.setNum(一0); p5.setNum(一0); p陆.setNum(一0); p漆.setNum(吧0); // 设置消费者产品消费数量 c一.setNum(50); c二.setNum(二0); c三.setNum(三0); // 线程始执行 c一.start(); c二.start(); c三.start(); p一.start(); p二.start(); p三.start(); p四.start(); p5.start(); p陆.start(); p漆.start(); }
全部回答
- 1楼网友:千杯敬自由
- 2021-11-25 01:18
http://blog.csdn.net/ghuil/article/details/41044257你参考下
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯