美文网首页
java消费者和生产者实现

java消费者和生产者实现

作者: exmexm | 来源:发表于2017-04-26 20:40 被阅读0次

    设计思想:
    1>确定生产对象馒头,设计馒头类。
    2>确定生产后存放馒头的篮子类。
    3>确定好生产者和消费者,并且确定这两个是使用Runnable接口的。

    注意要点:
    1>因为生产者和消费者都要对篮子类对象进行操作,所以最好对篮子类的方法都加锁(synchronized)
    2>鉴于当消费者消费完馒头,自己需要停下,调用Object的wait方法,并需要唤醒停下休息的生产者,所以要使用notify。
    生产者同理,当生产足够多的馒头就要停下,调用wait(),并唤醒其他线程notify.

    关于notify和wait以及condtion
    参考:http://www.cnblogs.com/dolphin0520/p/3920385.html

    
    public class ProducerComsumer {
    
        public static void main(String[] args) {
            SynStack ss = new SynStack();
            Consumer c1 = new Consumer(ss);
            Producer p1 = new Producer(ss);
            Thread t1 = new Thread(c1);
            Thread t2 = new Thread(p1);
            Thread t3 = new Thread(p1);
            t1.start();
            t2.start();
            t3.start();
        }
    }
        
        class WoTou {
            int id;
            
            WoTou (int id) {
                this.id = id;
            }
            
            public String toString() {
                return "馒头的编号为 : " + id + ".";
            }
        }
        
        
        class SynStack {
            int index = 0;
            WoTou[] ss = new WoTou[6];
            
            public synchronized void pop() {
                while (index == 0) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                this.notifyAll();
                index -- ;
                System.out.println("----消费---");
                System.out.println(ss[index].toString());
            }
            
            public synchronized void push(WoTou w) {
                while (index == ss.length) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                this.notifyAll();
                ss[index] =  w;
                System.out.println("----生产---");
                System.out.println(ss[index].toString());
                index ++;
            }
        }
        
        class Producer implements Runnable{
            public SynStack ss = null;
            
            Producer(SynStack ss) {
                this.ss = ss;
            }
    
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    WoTou w = new WoTou(i);
                    ss.push(w);
                    try {
                        Thread.sleep((int)Math.random()*10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        class Consumer implements Runnable {
            public SynStack ss = null;
    
            Consumer(SynStack ss) {
                this.ss = ss;
            }
            @Override
            public void run() {
                for(int i = 0; i < 80; i++) {
                    ss.pop();
                    try {
                        Thread.sleep((int)Math.random()*100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            
        }
    
    

    相关文章

      网友评论

          本文标题:java消费者和生产者实现

          本文链接:https://www.haomeiwen.com/subject/pxewzttx.html