java生产者消费者经典问题

来源:互联网 发布:软件程序开发 编辑:程序博客网 时间:2024/06/06 01:02
<h2><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 18px;"></span><h2>Java<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 12px;">生产者消费者的经典面试题实现</span></h2></h2>
/*@src  http://eric-619.iteye.com/blog/693681* 生产者消费者问题其含义就是先生产出了产品,才能拉出去让消费者购买* 一、重点:*    1、多个线程数据共享区域化思想!---源于多线程的近亲思想!!(类似于静态变量的改变)*    (如栈内存和对内存,还有当做栈内存和堆内存,如数组和基本数据类型,只要是访问的同一个。)*    2、生产者消费者*    * 二、synchronized加锁:* */public class ProCon{ //主方法public static void main(String[] args){SyncStack stack = new SyncStack();Consumer p = new Consumer(stack);Producer c = new Producer(stack);new Thread(p).start();new Thread(c).start();}}class Producer implements Runnable{   //生产者    private SyncStack stack;    public Producer(SyncStack stack){    this.stack = stack;     }    public void run(){    for (int i = 0; i < stack.pro().length; i++){    String product = "产品"+i;    stack.push(product);    System.out.println("生产了: "+product);    try{     Thread.sleep(200);     }catch(InterruptedException e)      {       e.printStackTrace();     }   }}}class Consumer implements Runnable{   //消费者   private SyncStack stack;   public Consumer(SyncStack stack) {   this.stack = stack;    }      public void run(){   for(int i = 0; i < stack.pro().length; i++){    String product = stack.pop();    System.out.println("消费了: "+product);    try{     Thread.sleep(1000);   }catch(InterruptedException e){     e.printStackTrace();     }    }   }}class SyncStack{   // 此类是(本质上:共同访问的)共享数据区域private String[] str = new String[10];    private int index;        public synchronized void push(String sst){ //供生产者调用    if(index == sst.length()){     try{      wait();     }catch(InterruptedException e){       e.printStackTrace();      }    }   this.notify(); //唤醒在此对象监视器上等待的单个线程   str[index] = sst;   index++;}   public synchronized String pop(){   //供消费者调用    if(index == 0){     try{      wait();      }catch (InterruptedException e){       e.printStackTrace();      }   }    notify();    index--;    String product = str[index];    return product;   }    public String[] pro(){ //就是定义一个返回值为数组的方法,返回的是一个String[]引用     return str;   //这是一个String[]引用   }}

 Jdk_7u4 的 API 文档中生产者消费者问题的代码样例,仅供参考:

class Producer implements Runnable {   private final BlockingQueue queue;   Producer(BlockingQueue q) { queue = q; }   public void run() {     try {       while (true) { queue.put(produce()); }     } catch (InterruptedException ex) { ... handle ...}   }   Object produce() { ... } } class Consumer implements Runnable {   private final BlockingQueue queue;   Consumer(BlockingQueue q) { queue = q; }   public void run() {     try {       while (true) { consume(queue.take()); }     } catch (InterruptedException ex) { ... handle ...}   }   void consume(Object x) { ... } } class Setup {   void main() {     BlockingQueue q = new SomeQueueImplementation();     Producer p = new Producer(q);     Consumer c1 = new Consumer(q);     Consumer c2 = new Consumer(q);     new Thread(p).start();     new Thread(c1).start();     new Thread(c2).start();   } }


0 0