【260天】我愛刷題系列(19)

叨叨兩句

  1. 今天好好整理下躍遷方法論,累P了。
  2. 身體的內置程序要調整啊,面對源源不斷的負面輸入,感受輸出的結果仍是不夠讓如今的我滿意,要調整到完全化解,並轉化爲積極能量才行。

生產者消費者模型相關案例(案例3)

案例3:電梯-物業人員-電梯間-人-大廳

  1. 電梯——消費者
  2. 物業人員——生產者
  3. 電梯間——緩衝區
  4. 人——數據
  5. 大廳——數據源

消費者

package com.test2;

import java.util.concurrent.CopyOnWriteArrayList;

public class Box extends Thread {
    
    //緩衝區
    public ElevatorRoom er;

    //上下行狀態標識
    private boolean flag = true;
    
    //電梯當前樓層
    private int currentLevel = 1;
    
    //用來檢測電梯是否裝滿的集合
    private CopyOnWriteArrayList<Person> list = new CopyOnWriteArrayList<>();
    
    //用來存儲電梯最高樓層
    private int maxCurrentLevel = 0;
    
    public Waiter waiter;
    
    public Box(ElevatorRoom er,Waiter waiter) {
        this.er = er;
        this.waiter = waiter;
    }

    //開門
    public void open() {
        System.out.println("電梯開門了");
    }
    
    //關門
    public void close() {
        System.out.println("電梯關門了");
    }
    
    //判斷是否運行
    public boolean isRunning() {
        if(er.queue.size() > 0 || waiter.count > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    //運行
    public void run() {
        while(isRunning()) {
            
            //將人裝入電梯
            while(list.size() < Configs.BOX_SIZE) {
                Person p = er.getPerson();
                p.enterBox();            
                list.add(p);
                if(waiter.count == 0) {
                    break;
                }
            }
            
            //找出電梯中的人要去的最高樓層
            for (Person person : list) {
                if(person.getToFloor() > maxCurrentLevel) {
                    maxCurrentLevel = person.getToFloor();
                }
            }
            
            //運行電梯
            while(true) {
                
                if(currentLevel == 1 && list.size() == 0) {
                    break;
                }
                
                if(flag) {
                    currentLevel++;
                } else {
                    currentLevel--;
                }
                
                if(currentLevel == maxCurrentLevel) {
                    flag = false;
                } 
                
                if(currentLevel == 1) {
                    flag = true; 
                }
                
                System.out.println("電梯運行到了" + currentLevel + "層");

                //到指定樓層出電梯
                for(Person p : list) {
                    if(currentLevel == p.getToFloor()) {
                        p.outBox();
                        list.remove(p);
                        waiter.count--;
                    }
                }
                
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }    
        }    
    }
}

緩衝區

package com.test2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ElevatorRoom {
    public BlockingQueue<Person> queue = new ArrayBlockingQueue<>(Configs.BOX_SIZE);

    public void putPerson(Person p) {
        try {
            queue.put(p);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }  
    
    public Person getPerson() {
        Person person = null;
        try {
            person = queue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return person;
    }
}

生產者

package com.test2;

public class Waiter extends Thread{
    ////緩衝區
    public ElevatorRoom er;
    public int No = 0;
    public int count = Configs.TOTAL;
    
    public Waiter(ElevatorRoom er) {
        this.er = er;
    }

    public void run() {
        while(count > 0) {
            Person p = new Person("name" + No);
            er.putPerson(p);
            No++;
            count--;
        }
    }
}
package com.test2;

public class Configs {
    public static final int TOTAL = 4;
    public static final int BOX_SIZE = 2;
}
package com.test2;

import java.util.Random;

public class Person {
    
    private String name;
    private int toFloor;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getToFloor() {
        return toFloor;
    }
    
    public void setToFloor(int toFloor) {
        this.toFloor = toFloor;
    }
    
    public Person(String name) {
        Random r = new Random();
        this.name = name;
        this.toFloor = r.nextInt(16) + 2;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", toFloor=" + toFloor + "]";
    }
    
    public void enterBox() {
        System.out.println(name + "準備去" + toFloor + "層");
    }
    
    public void outBox() {
        System.out.println(name + "到達了" + toFloor + "層");
    }
    
}
package com.test2;

public class Test {

    public static void main(String[] args) {
        ElevatorRoom br = new ElevatorRoom();
        Waiter waiter = new Waiter(br);
        Box box = new Box(br,waiter);
        box.start();
        waiter.start();
    }

}
相關文章
相關標籤/搜索