叨叨两句
- 今天好好整理下跃迁方法论,累P了。
- 身体的内置程序要调整啊,面对源源不断的负面输入,感觉输出的结果还是不够让现在的我满意,要调整到彻底化解,并转化为积极能量才行。
生产者消费者模型相关案例(案例3)
案例3:电梯-物业人员-电梯间-人-大厅
- 电梯——消费者
- 物业人员——生产者
- 电梯间——缓冲区
- 人——数据
- 大厅——数据源
消费者
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();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。