首先在blinkGreen添加一个布尔变量,并在blinkRed定时器这样,
在BlinkGreen:
boolean isRunningGreen = false;
Timer blinkGreen = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
// do stuff
isRunningGreen = true;//This statement must be placed at the end/bottom of this method
}
};
在BlinkRed:
boolean isRunningRed = false;
Timer blinkRed = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
// do stuff
isRunningRed = true;//This statement must be placed at the end/bottom of this method
}
};
现在试试这个:
for(int i=0;i<pcArray.length;i++){
if(pcArray[i]==1){
blinkGreen.start();
while(!isRunningGreen){}//waiting until the task is completed
} else if(pcArray[i]==2){
blinkRed.start();
while(!isRunningRed){}//waiting until the task is completed
}
}
不要使用循环,而是让TimerTask本身排队执行以下任务。或者只是在循环中进行睡眠。 – 2015-03-10 13:30:07