生产者消费者模式

生产者消费者模式

方式一:创建容器的方法

/*
 * 
 * 生产者消费者模式
 */
public class ProducerAndCustomerTest {
    public static void main(String[] args) {
        //先创建容器
        LinkedList 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 list;
    private int id =1;
    //构造方法获得list
    public Producer(LinkedList 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 list;
    
    public Customer(LinkedList 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();
                }
            }
            
        }
    }
    

你可能感兴趣的:(生产者消费者模式)