/*4、 采用Java 多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪膛)进行操作
其最大容量是12颗子弹。生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,
它不断从枪膛中射出子弹。(30分)
要求:
(1)给出分析过程说明。(10分)
(2)程序输出,要模拟体现对枪膛的压入和射出操作;(10)
(2)设计程序时应考虑到两个线程的同步问题。(10)
*/

解决方案 »

  1.   

    import java.util.Random;  
    import java.util.concurrent.BlockingQueue;  
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.LinkedBlockingQueue;  
    import java.util.concurrent.TimeUnit;  
      
    /** 
     * 4、   采用Java 多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪膛)进行操作, 
     * 其最大容量是12颗子弹。生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程, 
     * 它不断从枪膛中射出子弹。 
     *  要求: 
     *  (1)给出分析过程说明。 
     *  (2)程序输出,要模拟体现对枪膛的压入和射出操作; 
     * (2)设计程序时应考虑到两个线程的同步问题。 
     
     */  
    public class Gun {  
        class Bullet{  
            int id = bulletCounter++;  
            @Override  
            public String toString() {  
                // TODO Auto-generated method stub  
                return "" + id;  
            }  
        }  
        class Shooter implements Runnable{  
            BlockingQueue<Bullet> bullets;  
            Random r = new Random();  
            Shooter(BlockingQueue<Bullet> bullets){  
                this.bullets = bullets;  
            }  
            void shoot() throws InterruptedException{  
                Bullet bullet = bullets.take();  
                System.out.println("射出子弹:" + bullet + "---枪膛子弹数量:" + bullets.size());  
            }  
            @Override  
            public void run() {  
                try {  
                    while(!Thread.interrupted()){  
                        shoot();  
                        TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));  
                    }  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    //e.printStackTrace();  
                    System.out.println("停止射击");  
                }  
                  
            }  
              
        }  
        class BulletProductor implements Runnable{  
            BlockingQueue<Bullet> bullets;  
            Random r = new Random();  
            public BulletProductor(BlockingQueue<Bullet> bullets) {  
                this.bullets = bullets;  
            }  
            public void product(){  
                if(bullets.size() < BULLET_SIZE){//枪膛未满  
                    Bullet bullet = new Bullet();  
                    bullets.add(bullet);  
                    System.out.println("装入子弹:" + bullet + "---枪膛子弹数量:" + bullets.size());  
                }  
            }  
            @Override  
            public void run() {  
                try {  
                    while(!Thread.interrupted()){  
                        product();  
                        TimeUnit.MILLISECONDS.sleep(r.nextInt(500));  
                    }  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    //e.printStackTrace();  
                    System.out.println("停止装子弹");  
                }  
            }  
              
        }  
        static int bulletCounter = 0;  
        static final int BULLET_SIZE = 12;  
        BlockingQueue<Bullet> bullets;  
        ExecutorService exec;  
        Gun(ExecutorService exec){  
            this.bullets = new LinkedBlockingQueue<Bullet>();  
            this.exec = exec;  
        }  
        public void action(){  
            exec.execute(new BulletProductor(bullets));  
            exec.execute(new Shooter(bullets));  
        }  
        public static void main(String[] args) throws InterruptedException {  
            ExecutorService exec = Executors.newCachedThreadPool();  
            Gun gun = new Gun(exec);  
            gun.action();  
            TimeUnit.SECONDS.sleep(6);  
            exec.shutdownNow();  
            System.out.println("模拟结束");  
        }  
    }