美文网首页java学习记录
生产者消费者模式

生产者消费者模式

作者: 一花一世界yu | 来源:发表于2020-08-06 21:14 被阅读0次

    生产者消费者模式

    方式一:创建容器的方法

    /*
     * 
     * 生产者消费者模式
     */
    public class ProducerAndCustomerTest {
        public static void main(String[] args) {
            //先创建容器
            LinkedList<Egg> list = new LinkedList<>();
            Producer s1 = new Producer(list);
            Customer s2 = new Customer(list);
            //创建线程
            Thread t1 = new Thread(s1,"母鸡");
            Thread t2 = new Thread(s2,"消费者");
            //启动线程
            t1.start();
            t2.start();
        }
    }
    
    //产品类
    class Egg{
        private int id;
    
        public Egg(int id) {
            super();
            this.id = id;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "Egg [id=" + id + "]";
        }
        
    }
    
    
    //生产者线程
    class Producer implements Runnable{
        //设置一个集合作为属性一会存储元素
        private LinkedList<Egg> list;
        private int id =1;
        //构造方法获得list
        public Producer(LinkedList<Egg> list) {
            super();
            this.list = list;
        }
    
        @Override
        public void run() {
            //生产过程
            while(true){
                synchronized(list){
                    while(list.size() == 8){
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    //如果没满到这一步开始生产
                    Egg egg = new Egg(id++);
                    
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //存入容器
                    list.push(egg);
                    System.out.println(Thread.currentThread().getName() + "母鸡下蛋" + egg.getId());
                    list.notify();
                }
                
                
                
            }
            
            
            
            
        }
        
    }
    
    //消费者线程
    class Customer implements Runnable{
    
        private LinkedList<Egg> list;
        
        public Customer(LinkedList<Egg> list) {
            super();
            this.list = list;
        }
    
        @Override
        public void run() {
            while(true){
                
                synchronized(list){
                    while(list.size() == 0){
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                        Egg egg = list.pop();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + "消费者消费" + egg.getId());
                        list.notify();
                    }
                }
                
            }
        }
        
    
    

    相关文章

      网友评论

        本文标题:生产者消费者模式

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