📄 runpool.java
字号:
import java.util.*;
class PoolC
{
private int water=500;
int pushWater()
{
Random rd=new Random();
water=water+(rd.nextInt()>>>1)%600;
return water;
}
int popWater()
{
Random rd=new Random();
water=water-(rd.nextInt()>>>1)%600;
return water;
}
public void isAllow()
{
if(water<50)
{System.out.println("水库水位过底,禁止抽水!");}
if(water>=1000)
{System.out.println("水库超过警戒水位,禁止注水!");}
}
};
class stateA implements Runnable
{
private PoolC p;
int downwater;
public stateA(PoolC p)
{
this.p=p;
}
public void run()
{
while(true)
{
Random rd=new Random();
downwater=(rd.nextInt()>>>1)%1000;
try{
Thread.sleep(1000);
}
catch(Exception e){}
p.isAllow();//下层水库是否允许抽水
if(downwater<50)
{
System.out.println("下层水库水位过底,禁止抽水!");
}
else
{
p.pushWater();
System.out.println("泵站A注水中。");
}
}
}
};
class stateB implements Runnable
{
private PoolC pb;
int upwater;
public stateB(PoolC pc)
{
this.pb=pc;
}
public void run()
{
Random rd=new Random();
upwater=(rd.nextInt()>>>1)%1000;
while(true)
{
try{
Thread.sleep(1000);
}
catch(Exception e){}
pb.isAllow();//上层水库是否允许注水
if (upwater>1000)
{
System.out.println("上层水库超过警戒水位,禁止注水!");
}
else
{
pb.popWater();
System.out.println("泵站B抽水中。");
}
}
}
};
class RunPool
{
public static void main(String args[])
{
PoolC pc=new PoolC();
stateA as=new stateA(pc);
stateB bs=new stateB(pc);
Thread a=new Thread(as);
Thread b=new Thread(bs);
a.start();
b.start();
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -