首先在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