生产消费者模式

时间:2022-12-07 18:47:37

1.        一个场景,共同的资源

2.        生产者消费者模式信号灯法

3.        Wait()等待,释放锁,sleep,不释放锁

4.        Notify()/notifyall()唤醒

生产者和消费者使用相同的资源,多线程操作,怎么控制线程执行过程,可通过信号灯,

信号灯法

flag =True,生产者生产,消费者等待,生产者完成后通知消费者

flag=false,消费者消费,生产者等待,消费完成后通知生产者

共同的资源文件:

public class Movie {
    private String pic;
    private Boolean flag=true;
    //信号灯法
    //flag =True,生产者生产,消费者等待,生产者完成后通知消费者
    //flag=false,消费者消费,生产者等待,消费完成后通知生产者



    public synchronized void play(String pic){
        if(!flag){
            //生产者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else{
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Log.v("jiang","生产了"+pic);
        //生产完毕
        this.pic=pic;
        //通知消费者
        this.notify();
        //生产者停下
        this.flag=false;

    }
    public synchronized  void watch(){
        if(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else{
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Log.v("jiang","消费了"+pic);
        //通知生产者
        this.notify();
        //消费者停下
        this.flag=true;

    }
}

生产者:
public class Player  implements Runnable {
    private Movie m;

    public Player(Movie m) {
        this.m = m;
    }

    @Override
    public void run() {
       for(int i=0;i<30;i++){
           if(i%2==0){
               m.play("动画片");
           }
           else{
               m.play( "电影");
           }
       }
    }
}

消费者:
public class Wathcer implements Runnable {
    private Movie m;

    public Wathcer(Movie m) {
        this.m = m;
    }

    @Override
    public void run() {
        for(int i=0;i<30;i++){
            m.watch();
        }
    }
}

测试:
public void test() throws Exception{
    Movie m=new Movie();
    //多线程
    Player p=new Player(m);
    Wathcer w=new Wathcer(m);


    new Thread(p).start();
    new Thread(w).start();
}


运行结果:

08-03 12:57:49.68326253-26431/yubo.jipingtong V/jiang: 生产了动画片

08-03 12:57:49.99326253-26442/yubo.jipingtong V/jiang: 消费了动画片

08-03 12:57:49.99326253-26431/yubo.jipingtong V/jiang: 生产了电影

08-03 12:57:50.00326253-26442/yubo.jipingtong V/jiang: 消费了电影

08-03 12:57:50.00326253-26431/yubo.jipingtong V/jiang: 生产了动画片

08-03 12:57:50.00326253-26442/yubo.jipingtong V/jiang: 消费了动画片

08-03 12:57:50.00326253-26431/yubo.jipingtong V/jiang: 生产了电影

08-03 12:57:50.00326253-26442/yubo.jipingtong V/jiang: 消费了电影

08-03 12:57:50.00326253-26431/yubo.jipingtong V/jiang: 生产了动画片

08-03 12:57:50.00326253-26442/yubo.jipingtong V/jiang: 消费了动画片

08-03 12:57:50.00326253-26431/yubo.jipingtong V/jiang: 生产了电影

08-03 12:57:50.00326253-26442/yubo.jipingtong V/jiang: 消费了电影

08-03 12:57:50.00326253-26431/yubo.jipingtong V/jiang: 生产了动画片