消费者和生产者问题,管程法和信号灯法

消费者和生产者问题

有两种方法,一种是管程法,一种是信号灯法,个人感觉区别不是很大,区别就在于信号灯法使用的是标志位控制,管程法是通过变量的值控制

废话少说,直接上代码

管程法

public class test1 {
    
    
    public static void main(String[] args) {
    
    
        Behavior Behavior = new Behavior(10);
        new person(Behavior).start();
        new chan(Behavior).start();
    }
}

/**
 * 消费者类
 * 消费者只管消费
 */
class person extends Thread{
    
    
    Behavior Behavior;
    public person(Behavior Behavior){
    
    
        this.Behavior=Behavior;
    }
    @Override
    public void run() {
    
    
        for (int i = 0; i < 20; i++) {
    
    
            Behavior.xiaofei();//消费
        }
    }
}

/**
 * 生产者类
 * 生产者只管生产
 */
class chan extends Thread{
    
    
    public chan(Behavior Behavior){
    
    
        this.Behavior=Behavior;
    }
    Behavior Behavior;
    @Override
    public void run() {
    
    
        for (int i = 0; i < 20; i++) {
    
    
            Behavior.shengchan();//生产
        }
    }
}

/**
 * 行为类
 */
class Behavior {
    
    
    private int size;//设置缓存区最大容量,LinkedList没有容量限制,所以要设置一个最大容量来控制
    LinkedList<Integer> list=new LinkedList<>();//设置缓存区(容纳区)
    
    public Behavior(int size) {
    
    
        this.size = size;
    }

    /**
     * 生产方法
     */
    public synchronized void shengchan (){
    
    
       if (list.size()==size){
    
    //缓存区满了,就不生产了
           try {
    
    
               this.wait();
           } catch (InterruptedException e) {
    
    
               e.printStackTrace();
           }
       }
       this.notify();
       list.push(1);//生产
       System.out.println("生产了一个,目前有"+list.size());
    }

    /**
     * 消费方法
     */
    public synchronized void xiaofei(){
    
    
        if (list.size()==0){
    
    //缓存区吃空了,就不吃了
            try {
    
    
                this.wait();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        this.notify();
        list.removeFirst();//吃
        System.out.println("消费者吃了一个,目前"+list.size());
    }
}

信号灯法

public class test {
    
    
   public static void main(String[] args) {
    
    
       TV tv = new TV("娱乐节目");
       new Actor(tv).start();
       new People(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++) {
    
    
           tv.act();
       }
   }
}

/**
* 观众类
*/
class People extends Thread{
    
    
   TV tv;
   public People(TV tv){
    
    
       this.tv=tv;
   }
   @Override
   public void run() {
    
    
       for (int i = 0; i < 10; i++) {
    
    
           tv.lock();
       }
   }
}

/**
* 电视类
*/
class TV{
    
    
   private String program;//演出的节目名称
   private boolean have=false;//标志位,是否有节目可以看

   public TV(String program) {
    
    
       this.program = program;
   }

   public synchronized void act(){
    
    
       if (have){
    
    //有就不演
           try {
    
    
               this.wait();
           } catch (InterruptedException e) {
    
    
               e.printStackTrace();
           }
       }
       this.notify();
       have=true;//演了就有节目了
       System.out.println("演员演出了"+program);
   }
   public synchronized void lock(){
    
    
       if (!have){
    
    //没有就不看
           try {
    
    
               this.wait();
           } catch (InterruptedException e) {
    
    
               e.printStackTrace();
           }
       }
       this.notify();
       have=false;//看了就没节目了
       System.out.println("观众观看了"+program);
   }
}

希望能帮到大家

猜你喜欢

转载自blog.csdn.net/qq_44769485/article/details/108555534