线程协作和通信问题-生产者和消费者问题

线程协作和通信问题

生产者和消费者问题

  1. 解决办法一:管程法:设置缓冲区
package Gcf;



//生产者,消费者,产品,缓冲区
public class TestGcf {
    
    
    public static void main(String[] args) {
    
    
        Hc hc = new Hc();

        new Productor(hc).start();
        new Cum(hc).start();
    }
}
//生产者
class Productor extends Thread{
    
    
    Hc hc;
    public Productor(Hc hc) {
    
    
        this.hc = hc;
        }

    @Override
    public void run() {
    
    
        for (int i = 1; i < 100; i++) {
    
    
            hc.push(new Chickent(i));
            System.out.println("生产了"+i+"只鸡");
        }
    }
}
//消费者
class Cum extends Thread{
    
    
    Hc hc;
    public Cum(Hc hc) {
    
    
        this.hc = hc;
    }

    @Override
    public void run() {
    
    
        for (int i = 1; i < 100; i++) {
    
    
            System.out.println("消费了——》"+hc.pop().id+"只鸡");
        }
    }
}
//产品
class Chickent{
    
    
    int id;
    public Chickent(int id) {
    
    
        this.id = id;
    }
}
//缓冲区
class Hc{
    
    
    //缓冲区大小
    Chickent[] chickents=new Chickent[10];
    //计数器
    int count;
    //生产者生产
    public synchronized void push(Chickent chickent){
    
    
        //什么时候生产
        if(count==chickents.length){
    
    
            //如果缓冲区满了,就等待消费者消费
            try {
    
    
                this.wait();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        //缓冲区没满
        chickents[count]=chickent;
        count++;
        //通知消费者消费
        this.notifyAll();
    }
    //消费者消费
    public synchronized Chickent pop(){
    
    
        //什么时候消费
        if(count==0){
    
    
            try {
    
    
                this.wait();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        //如果缓冲区有产品就消费
        count--;
        Chickent chickent = chickents[count];
        this.notifyAll();
        return chickent;
    }

}

  1. 解决办法二:**信号灯法:**设置标志位

    package xhd;
    //信号灯法  标志位解决 T F
    //看电视   观众 演员 电视
    public class Xhd {
          
          
        public static void main(String[] args) {
          
          
            Tv tv = new Tv();
            new Person(tv).start();
            new Actor(tv).start();
        }
    }
    //演员
    class Actor extends Thread{
          
          
        Tv tv;
        public Actor(Tv tv) {
          
          
            this.tv = tv;
        }
    
        @Override
        public void run() {
          
          
            for (int i = 0; i < 10; i++) {
          
          
                if(i%2==0){
          
          
                   this.tv.push("快本播放中");
                }else{
          
          
                this.tv.push("快手");
            }
            }
        }
    }
    
    //观众
    class Person extends Thread{
          
          
        Tv tv;
        public Person(Tv tv) {
          
          
            this.tv = tv;
        }
    
        @Override
        public void run() {
          
          
            for (int i = 0; i < 10; i++) {
          
          
                tv.watch();
            }
        }
    }
    
    
    //电视
    class Tv{
          
          
        String viose;//表演的节目
        //演员表演 观众等待 T
        //观众观看 演员等待 F
        boolean flag=true;//设置标志位
        //演员表演
        public synchronized void push(String viose){
          
          
            if(!flag){
          
          
                try {
          
          
                    this.wait();
                } catch (InterruptedException e) {
          
          
                    e.printStackTrace();
                }
            }
            //什么时候表演
            System.out.println("演员正在表演"+viose);
            //通知观众观看
            this.notifyAll();
            this.viose=viose;
            this.flag=!this.flag;
    
        }
        //观众观看
        public synchronized void watch(){
          
          
            //什么时候观看
            if(flag){
          
          //演员正在表演
                try {
          
          
                    this.wait();
                } catch (InterruptedException e) {
          
          
                    e.printStackTrace();
                }
            }
            System.out.println("观众观看了"+viose);
            this.notifyAll();
            this.flag=!this.flag;
    
        }
    }
    
    

Xinyue Day3

猜你喜欢

转载自blog.csdn.net/qq_47735503/article/details/110580683