📄 waterpumper.java
字号:
import java.lang.Thread;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class WaterPumper {
private static int MAX_SIZE = 5000;
private static int MIN_SIZE = 50;
private static int TIME_SLEEP = 1500;
private static int TIME_WORK = 5;
private static int UNIT = 50;
private static int CURRENT_SIZE = MIN_SIZE;
private static int pumpOutTime;
private static int pumpInTime;
private static int type = 1;
private PumpOut pumpOut;
private PumpIn pumpIn;
private Timer timer;
private Random rand ;
private ContainerC container;
public static void main(String[] args) {
WaterPumper pumper = new WaterPumper();
pumper.run();
}
public WaterPumper() {
container = new ContainerC();
pumpOut = new PumpOut(container);
pumpIn = new PumpIn(container);
timer = new Timer();
rand = new Random();
pumpOutTime = rand.nextInt(TIME_WORK);
pumpInTime = rand.nextInt(TIME_WORK);
if (pumpOutTime <= 0) {
pumpOutTime = rand.nextInt(TIME_WORK);
}
if (pumpInTime <= 0) {
pumpInTime = rand.nextInt(TIME_WORK);
}
System.out.println("first pumpOutTime and pumpInTime are :" + pumpOutTime +" " + pumpInTime);
}
public void run() {
try {
pumpOut = new PumpOut(container);
pumpIn = new PumpIn(container);
timer.schedule(pumpOut, 1500, pumpOutTime);
timer.schedule(pumpIn, 1500, pumpInTime);
new Thread(pumpOut).start();
new Thread(pumpIn).start();
} catch (Exception e) {
System.out.println("error :" + e);
}
}
protected class ContainerC extends TimerTask{
private int tmp;
private int pumpTime;
protected void runOut() {
System.out.println("working in PumpOut for period of :" + WaterPumper.pumpOutTime);
pumpTime = rand.nextInt(WaterPumper.TIME_SLEEP);
while (true) {
tmp = CURRENT_SIZE - WaterPumper.UNIT;
if (tmp >= WaterPumper.MIN_SIZE) {
CURRENT_SIZE -= WaterPumper.UNIT;
System.out.println(" 从水库抽取水后,水量为:" + CURRENT_SIZE);
} else {
try {
System.out.println("sleep for pumpTime:" + pumpTime);
Thread.sleep(pumpTime);
System.out.println("yield to another Thread--------------");
Thread.currentThread().yield();
pumpTime = rand.nextInt(WaterPumper.TIME_SLEEP);
pumpOutTime = rand.nextInt(WaterPumper.TIME_SLEEP);
} catch (Exception e) {
System.out.println("Error :" + e);
}
}
}
}
protected void runIn() {
System.out.println("working in PumpIn for period of :" + WaterPumper.pumpInTime);
pumpTime = rand.nextInt(WaterPumper.TIME_SLEEP);
while (true) {
tmp = CURRENT_SIZE + WaterPumper.UNIT;
if (tmp <= WaterPumper.MAX_SIZE) {
CURRENT_SIZE = CURRENT_SIZE + UNIT;
System.out.println(" 水库内注入水后,水量为:" + CURRENT_SIZE);
} else {
try {
System.out.println("sleep for pumpTime:" + pumpTime);
Thread.sleep(pumpTime);
System.out.println("yield to another Thread--------------");
Thread.currentThread().yield();
pumpTime = rand.nextInt(WaterPumper.TIME_SLEEP);
pumpInTime = rand.nextInt(WaterPumper.TIME_SLEEP);
} catch (Exception e) {
System.out.println("Error :" + e);
}
}
}
}
public void run() {
if (WaterPumper.type == 1) {
runOut();
pumpOut.finalize();
}else if(WaterPumper.type == 2) {
runIn();
pumpIn.finalize();
}
}
}
protected class PumpOut extends TimerTask implements Runnable {
private ContainerC container;
public PumpOut(ContainerC container) {
this.container = container;
}
public void run() {
WaterPumper.type = 1;
container.run();
}
protected void finalize() {
System.out.println("PumpOut is cleared---------------");
}
}
protected class PumpIn extends TimerTask implements Runnable{
private ContainerC container;
public PumpIn(ContainerC container) {
this.container = container;
}
public void run() {
WaterPumper.type = 2;
container.run();
}
protected void finalize() {
System.out.println("PumpIn is cleared---------------");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -