Java简单实现生产者消费者问题(源自2015年阿里巴巴实习生校园招聘)

来源:互联网 发布:淘宝网不能正常显示 编辑:程序博客网 时间:2024/05/24 04:41
package test;


  
//主类  可以用生产者消费者问题模拟该生产苹果和取走苹果的过程
class  ProducerConsumer  
{  
    public static void main(String[] args)   
    {  
        CommonStack s = new CommonStack();  
        Producer p = new Producer(s);  
        Consumer c = new Consumer(s);  
        Thread tp = new Thread(p);  
        Thread tc = new Thread(c);  
        tp.start();  
        tc.start();  
    }  
}  
  
//  
class Apple   //苹果类
{  
    private int id;  
      
    Apple(int id){  
        this.id = id;  
    }  
  
    public String toString(){  
        return "Apple :" + id;  
    }  
}  
  
//共享栈空间  
class CommonStack  
{  
    Apple sm[] = new Apple[5];  //5个空间大小的共享栈空间
    int index = 0;  
      
    /**  
     生产方法. 
    */   
  
    public synchronized void push(Apple m){  
        try{  
            while(index == sm.length){  
                System.out.println("!!!!!!!!!苹果满了!!!!!!!!!");  
                this.wait();  
            }  
            this.notify();  
        }catch(InterruptedException e){  
            e.printStackTrace();  
        }catch(IllegalMonitorStateException e){  
            e.printStackTrace();  
        }  
          
        sm[index] = m;  
        index++;  
        System.out.println("生产了:" + m + " 共" + index + "个苹果");  
    }  
  
    /**
     * 取走苹果,消费方法
     * 
     */
    public synchronized Apple pop(){  
        try{  
            while(index == 0){  
                System.out.println("!!!!!!!!!苹果取光了!!!!!!!!!");  
                this.wait();  
            }  
            this.notify();  
        }catch(InterruptedException e){  
            e.printStackTrace();  
        }catch(IllegalMonitorStateException e){  
            e.printStackTrace();  
        }  
        index--;  
        System.out.println("取走了:---------" + sm[index] + " 共" + index + "个苹果");  
        return sm[index];  
    }  
}  
  
class Producer implements Runnable   //实现Runnable接口,代理模式
{  
    CommonStack ss = new CommonStack();  
    Producer(CommonStack ss){  
        this.ss = ss;  
    }  
  
    /**  
    * show 生产进程.  
    */   
    public void run(){   //改写run()
        for(int i = 0;i < 20;i++){  
            Apple m = new Apple(i);  
            ss.push(m);  
//       
            try{  
                Thread.sleep((int)(Math.random()*500));  
            }catch(InterruptedException e){  
                e.printStackTrace();  
            }  
        }  
    }  
}  
  
class Consumer implements Runnable  //同理
{  
    CommonStack ss = new CommonStack();  
    Consumer(CommonStack ss){  
        this.ss = ss;  
    }  
  
    /**  
    * show 消费进程. 
    */   
    public void run(){  
        for(int i = 0;i < 20;i++){  
            Apple m = ss.pop();  


            try{  
                Thread.sleep((int)(Math.random()*1000));  
            }catch(InterruptedException e){  
                e.printStackTrace();  
            }  
        }  
    }  
}  
0 0
原创粉丝点击