package test;
/**
*
* @author ZMK
*
* <br> 一栋房子有一壶水,猫和狗一起喝。</br>
* 狗喝的比猫快,直到渴死
*/
public class House implements Runnable {
int water ;
public void setWater(int water) {
this.water = water;
}
@Override
public void run() {
while(true){
String name = Thread.currentThread().getName();
if(name.equals("狗")){
System.out.println(name + "喝水");
water = water - 2;
}
else if (name.equals("猫")) {
System.out.println(name + "喝水");
water = water - 1;
public static void main(String[] args) {
House house = new House();
house.setWater(10);
Thread dog = new Thread(house);
Thread cat = new Thread(house);
dog.setName("狗");
cat.setName("猫");
dog.start();
cat.start();
}