?? runpool.java
字號(hào):
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("水庫(kù)水位過(guò)底,禁止抽水!");}
if(water>=1000)
{System.out.println("水庫(kù)超過(guò)警戒水位,禁止注水!");}
}
};
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();//下層水庫(kù)是否允許抽水
if(downwater<50)
{
System.out.println("下層水庫(kù)水位過(guò)底,禁止抽水!");
}
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();//上層水庫(kù)是否允許注水
if (upwater>1000)
{
System.out.println("上層水庫(kù)超過(guò)警戒水位,禁止注水!");
}
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();
};
};
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -