Java 写一个生产者和消费者的多线程Demo
2012-08-10 15:13
316 查看
/** * 文件名: Customer.java * * 顾客 * **/ public class Customer{ @SuppressWarnings("unused") private Shop shop = null; private String name = null; public void buy(){ int count = shop.getBreadCount(); if(count > 0){ shop.setBreadCount(--count); System.out.println(name +" says : I bought a bread, now the remained quantity is : "+ count +"!"); }else { System.out.println(name + " says : What a pity, there is no bread left!----------------------"); } } public Customer(Shop shop, String name){ this.shop = shop; this.name =name; } }
/** * 文件名: CustomerGenerator.java * * 顾客生成器 * **/ public class CustomerGenerator extends Thread { private Shop shop = null; public CustomerGenerator(Shop shop){ this.shop = shop; } public void run() { Customer customer = null; int count = 1; while(shop.isOpen()){ customer = new Customer(shop, count+"顾客"); customer.buy(); customer = null; count++; try { Thread.sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } } } }
/** * 文件名: Producer.java * * 生产者 * **/ public class Producer extends Thread { @SuppressWarnings("unused") private Shop shop = null; private String name = null; public void produce(){ int count = shop.getBreadCount(); shop.setBreadCount(++count); System.out.println(Thread.currentThread().getName()+" says : I produced a bread, now the count is : "+ count); } public Producer(Shop shop, String name){ this.shop = shop; super.setName(name); } public void run() { Random random = new Random(); while(shop.isOpen()){ this.produce(); try { int speed = 1500 + random.nextInt(1000); Thread.sleep(speed);//生产面包的速度 } catch (InterruptedException e) { e.printStackTrace(); } } } }
/** * 文件名: Shop.java * * 面包店 * **/ public class Shop { private int breadCount = 0;//当前面包店剩余的面包数量 private boolean isOpen = true;//是否正在营业 public synchronized int getBreadCount() { return breadCount; } public synchronized void setBreadCount(int breadCount) { this.breadCount = breadCount; } public boolean isOpen() { return isOpen; } public void setOpen(boolean isOpen) { this.isOpen = isOpen; } }
** * 文件名: ShopManager.java * * 生产消费者管理 * **/ public class ShopManager { public static void main(String[] args) { Shop shop = new Shop(); Producer pc1 = new Producer(shop, "1#面点师"); Producer pc2 = new Producer(shop, "2#面点师"); Producer pc3 = new Producer(shop, "3#面点师"); CustomerGenerator customerGenerator = new CustomerGenerator(shop); pc1.start(); pc2.start(); pc3.start(); customerGenerator.start(); try { Thread.sleep(30000);//工作时间 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("I' m sorry, 下班啦..."); shop.setOpen(false); } }
相关文章推荐
- java多线程之-- 一个生产者与一个消费者
- Java多线程之生产者消费者demo
- Java 多线程学习之生产者消费者模型:一个较完善的实现
- Java多线程的一个案例-生产者消费者问题
- java 多线程的生产者-消费者 实现
- java多线程-消费者和生产者模式
- Java多线程:生产者及消费者
- 编写一个生产者,消费者多线程程序,一个线程随机产生数据,另一个线程显示所产生的数据.
- java多线程之消费者生产者模式 (转)
- Java 多线程-生产者、消费者
- java实现线程同步一个生产者和一个消费者
- java多线程之生产者消费者经典问题
- 生产者与消费者问题(java多线程实现)
- JAVA笔记14__多线程共享数据(同步)/ 线程死锁 / 生产者与消费者应用案例 / 线程池
- Java多线程 - 实现生产者与消费者模式
- java多线程生产者、消费者模型
- Java多线程之~~~~使用wait和notify实现生产者消费者模型
- 由生产者/消费者问题看JAVA多线程
- 多线程经典案例——生产者/消费者问题的Java实现与详解
- Java 多线程:生产者消费者模型