package edu.mama.ls18.homework;
/*
兔子
*/
_public class HareRunnable implements Runnable {
private Race race;
public HareRunnable(Race race) {
if (race == null) {
throw new IllegalArgumentException(“race不能为null”);
}
this.race = race;
}
@Override
public void run() {
//兔子状态,每秒+1…-5~-1表示奔跑 非负表示休息;状态=0需要随机决定休息时长;状态=休息时长 休息结束
int state = -5;
//兔子休息时间
int rest=Integer._MAX_VALUE;
while (race.isPlaying){<br /> if(state<0) {<br /> race.hareDistance += 3;<br /> System._out_.println("兔子:奔跑");<br /> }else if(state==0){<br /> rest=(int)(Math._random_()*16+5); //[0,16)+5->[5,21)->int强制转换为[5,20]内整数<br /> System._out_.println("兔子:需要休息"+rest);<br /> System._out_.println("兔子:休息");<br /> }<br /> else{<br /> System._out_.println("兔子:休息");<br /> }
state++;<br /> if(state==rest) state=-5;
try {<br /> Thread._sleep_(1000);<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }
}<br /> }<br />}