水箱类代码:
package Anli.scz_xfz.givewater;
//水箱类
public class BOX {
//定义一个变量表示水的多少
private int water;
//创建布尔型变量,表示水箱的状态
private boolean state = true;
//把水装入水箱的方法
//由于涉及到多线程的数据安全问题,这里的方法需使用同步方法synchronized
public synchronized void put(int water){
//如果没有水,就送来水灌入
if(state) {
this.water = water;
System.out.println("水箱被灌入第" + this.water + "次送来的1吨水!");
//水送来了,就修改水箱状态为有水
state = false;
//唤醒其它所有线程来取水
notifyAll();
}
//如果有水,送水工就休息,等待取水
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//把水取出水箱的方法
public synchronized void get(){
//如果有水,就取水
if(!state) {
System.out.println("浇庄稼用掉了第" + this.water +