生产者消费者模式

来源:互联网 发布:关于淘宝兼职 编辑:程序博客网 时间:2024/05/16 14:03

        有一个筐,筐左边一个人,筐右边一个人,左边这个人一个个向筐里面放苹果,右边这个人一个个从筐里取苹果,周而复始。写一个独立的console程序,能够自动把该场景模拟出来,请描述实现思路,列出哪些类和对象,描述类的特点。

代码一:

import java.util.LinkedList;import java.util.List;public class TestPutGetApple {List<Apple> aplleLs = null;private int appleSize;public int getAppleSize(){return appleSize;}public TestPutGetApple(int appleSize) {this.appleSize = appleSize;aplleLs = new LinkedList<Apple>();}public static void main(String[] args) {TestPutGetApple tpga = new TestPutGetApple(100);PutAppleRunnable par = new PutAppleRunnable(tpga);GetAppleRunnable gar = new GetAppleRunnable(tpga);new Thread(par).start();new Thread(gar).start();}}class Apple {static int count = 0;final int id =count++;public int getId() {return id;}}class PutAppleRunnable implements Runnable {int putAppleCount = 0;TestPutGetApple tpga;public PutAppleRunnable(TestPutGetApple tpga){this.tpga = tpga;}@Overridepublic void run() {while(putAppleCount<tpga.getAppleSize()) {//synchronized (tpga){Apple ap = new Apple();tpga.aplleLs.add(ap);putAppleCount++;System.out.println("放进一个苹果,苹果ID是:"+ap.getId()+"苹果框里共有"+ tpga.aplleLs.size() +"个苹果");try {Thread.sleep((int)(Math.random()*2000));}catch(InterruptedException e) {e.printStackTrace();}//}}}}class GetAppleRunnable implements Runnable {TestPutGetApple tpga;public GetAppleRunnable(TestPutGetApple tpga) {this.tpga = tpga;}@Overridepublic void run() {while(true) {//synchronized (tpga){if(!tpga.aplleLs.isEmpty()){Apple ap = tpga.aplleLs.remove(tpga.aplleLs.size()-1);System.out.println("拿出一个苹果,苹果ID是:"+ ap.getId()+"苹果框里共有"+ tpga.aplleLs.size() +"个苹果");try {Thread.sleep((int)(Math.random()*2000));}catch(InterruptedException e) {e.printStackTrace();}}//}}}}

代码二、

public class ProducerConsumer {public static void main(String[] args) {SyncStack ss = new SyncStack();Producer p = new Producer(ss);Consumer c = new Consumer(ss);new Thread(p).start();new Thread(p).start();new Thread(p).start();new Thread(c).start();}}class WoTou {int id; WoTou(int id) {this.id = id;}public String toString() {return "WoTou : " + id;}}class SyncStack {int index = 0;WoTou[] arrWT = new WoTou[6];public synchronized void push(WoTou wt) {while(index == arrWT.length) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}this.notifyAll();arrWT[index] = wt;index ++;}public synchronized WoTou pop() {while(index == 0) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}this.notifyAll();index--;return arrWT[index];}}class Producer implements Runnable {SyncStack ss = null;Producer(SyncStack ss) {this.ss = ss;}public void run() {for(int i=0; i<20; i++) {WoTou wt = new WoTou(i);ss.push(wt);System.out.println("生产了:" + wt);try {Thread.sleep((int)(Math.random() * 200));} catch (InterruptedException e) {e.printStackTrace();}}}}class Consumer implements Runnable {SyncStack ss = null;Consumer(SyncStack ss) {this.ss = ss;}public void run() {for(int i=0; i<20; i++) {WoTou wt = ss.pop();System.out.println("消费了: " + wt);try {Thread.sleep((int)(Math.random() * 1000));} catch (InterruptedException e) {e.printStackTrace();}}}}

代码三、

import java.util.ArrayList;import java.util.List;public class TestPutGetApple {public static void main(String[] args) {AppleStack as = new AppleStack(10);PutApple pa = new PutApple(as);GetApple ga = new GetApple(as);new Thread(pa).start();new Thread(pa).start();new Thread(pa).start();new Thread(ga).start();}}class Apple {      static int count = 0;      final int id = count++;      public int getId() {          return id;      }  }class AppleStack {int curSize = 0;int maxSize;List<Apple> al = new ArrayList<Apple>();AppleStack(int maxSize) {this.maxSize = maxSize;}public synchronized  void put(Apple a){while(curSize>=maxSize) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}this.notifyAll();al.add(a);curSize++;}public synchronized Apple get(){while(curSize==0){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}this.notifyAll();curSize--;return al.remove((int)(Math.random()*(al.size()-1)));}}class PutApple implements Runnable{AppleStack as = null;PutApple(AppleStack as){this.as = as;}@Overridepublic void run() {while(true) {Apple a = new Apple();as.put(a);System.out.println("放进去一个苹果,苹果ID为"+ a.getId() +"共有苹果数:"+as.al.size());try {Thread. sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}class GetApple implements Runnable{AppleStack as = null;GetApple(AppleStack as){this.as = as;}@Overridepublic void run() {while(true) {Apple a = as.get();System.out.println("拿出去一个苹果,苹果ID为"+ a.getId() +"共有苹果数:"+as.al.size());try {Thread. sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}

模拟5个窗口卖火车票

import java.util.ArrayList;import java.util.List;public class SaleTickets {public static void main(String[] args) {Tickets ts = new Tickets();ts.initTicket();SallThread st = new SallThread(ts);new Thread(st,"Thread1").start();new Thread(st,"Thread2").start();new Thread(st,"Thread3").start();new Thread(st,"Thread4").start();new Thread(st,"Thread5").start();}}class Ticket{private  int id;Ticket(int id) {this.id = id;}public int getId(){return id;}}class Tickets{List<Ticket> lt = new ArrayList<Ticket>();public void initTicket(){for(int i=0;i<100;i++){Ticket t = new Ticket(i);lt.add(t);}}public synchronized Ticket sall(){if(!lt.isEmpty()){return lt.remove((int)Math.random()*(lt.size()-1));}else return null;}}class SallThread implements Runnable{Tickets ts = null;SallThread(Tickets ts){this.ts = ts;}@Overridepublic void run() {Ticket t = null;while(true) {if(ts.lt.size()>0) {t = ts.sall();System.out.println(Thread.currentThread().getName()+" sale a ticket which its id is" + t.getId() );}else {System.out.println(Thread.currentThread().getName()+" sale out!");break;}}}}



原创粉丝点击