首页 > 代码库 > 生产者-消费者模式
生产者-消费者模式
- 生产者负责生产产品。
- 消费者负责取走并使用产品。
- 生产者生产完成后通知消费者可以取走产品了。
- 消费者消费完产品后需要通知生产者生产产品。
- 生产者没有生产完成,消费者不能取走产品。
- 消费者没有使用完产品,生产者不能生产产品。
package main;import java.util.*;class TicketSeller implements Runnable{ public static Integer ticketSoldTotal = 0; private Integer totalTicket = 300; private Random random = new Random(); @Override public void run() { for(int i = 0; i < 500; i++) { synchronized(this) { if(this.totalTicket > 0) { Util.sleep(500); int ticketSold = random.nextInt(5); ticketSold++; if((this.totalTicket - ticketSold) < 0) { ticketSold = this.totalTicket; } ticketSoldTotal += ticketSold; this.totalTicket -= ticketSold; System.out.println(Thread.currentThread().getName()+" sells "+ticketSold+" ticket(s), " + this.totalTicket + " left. "); } } } }}class Util{ public static void sleep(Integer sleepTime) { try { Thread.sleep(sleepTime); } catch(Exception e) {} }}class Resource{ private String name; public Resource(String name) { this.name = name; } public String getName() { return this.name; }}class WritePaper implements Runnable{ private Resource resourceA; private Resource resourceB; public WritePaper(Resource resourceA, Resource resourceB) { super(); this.resourceA = resourceA; this.resourceB = resourceB; } public void run() { synchronized(this.resourceA) { Util.sleep(2000); System.out.println(Thread.currentThread().getName() + "" + this.resourceA.getName()); synchronized(resourceB) { System.out.println(Thread.currentThread().getName() + "" + this.resourceB.getName()); } } }}class Product{ private String name; private String desc; private Boolean readyToGet = false; public Product() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public synchronized void setInfo(String name, String desc) { if(this.readyToGet == true) { try { super.wait(); } catch(Exception e) {} } this.setName(name); Util.sleep(400); this.setDesc(desc); this.readyToGet = true; super.notifyAll(); } public synchronized String getInfo() { if(this.readyToGet == false) { try { super.wait(); } catch(Exception e) {} } String name = this.getName(); Util.sleep(300); String desc = this.getDesc(); this.readyToGet = false; super.notifyAll(); return "Name: " + name + " Desc: " + desc; } @Override public String toString() { return "Product [name=" + name + ", desc=" + desc + "]"; } }class Produce implements Runnable{ private Product product; public Produce(Product product) { super(); this.product = product; } @Override public void run() { for(int i = 0; i < 30; i++) { if(i%2 == 0) this.product.setInfo("fan", "Thre gorges"); else this.product.setInfo("computer", "HP"); } }}class Comsume implements Runnable{ private Product product; public Comsume(Product product) { super(); this.product = product; } @Override public void run() { for(int i = 0; i < 30; i++) { System.out.println(this.product.getInfo()); } } }public class main{ public static void main(String[] args) throws Exception { Product product = new Product(); Comsume c = new Comsume(product); Produce p = new Produce(product); List<Thread> threadList = new ArrayList<Thread>(); threadList.add(new Thread(p)); threadList.add(new Thread(c)); for(Thread thread : threadList) thread.start(); for(Thread thread : threadList) thread.join(); System.out.println("///~Main done"); } public static List<Integer> getDemoList() { Random random = new Random(); List<Integer> list = new ArrayList<>(); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); list.add(random.nextInt(30)); return list; }}
生产者-消费者模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。