我的条件必须每600毫秒发生一次。 我每25毫秒进入一次线程,检查我的时间量是否达到600以使条件发生,然后重置标志和值。
@Override
public void run() {
while (runs__) {
try {
Thread.sleep(25);
timeAmount__ = timeAmount__ + delta__;
Log.e("process"," " + k++ );
} catch (InterruptedException e) {
e.printStackTrace();
throw new NullPointerException();
}
if (delta__ <= 0)
timeAmount__ = 0;
else
runOnUiThread(new Runnable() {
@Override
public void run() {
// if (timeAmount__ >= 567 && timeAmount__ < 592) {
if (timeAmount__ >= 597 && timeAmount__ < 622) {
if (wrongAnswered) {
System.out.println("wrong answered: line 885");
class Ender implements Runnable {
public void run() {
imagesList.get(lastCells.get(lastCells.size() - 2)).setImageDrawable(null);
imagesList.get(lastCells.get(lastCells.size() - 1)).setImageDrawable(null);
imagesList.get(lastCells.get(lastCells.size() - 2)).setBackgroundResource(intAnimationsOff[displayedCharactersIndexesList.get(lastCells.get(lastCells.size() - 2))]);
imagesList.get(lastCells.get(lastCells.size() - 1)).setBackgroundResource(intAnimationsOff[displayedCharactersIndexesList.get(lastCells.get(lastCells.size() - 1))]);
AnimationDrawable animation1 = (AnimationDrawable) imagesList.get(lastCells.get(lastCells.size() - 2)).getBackground();
AnimationDrawable animation2 = (AnimationDrawable) imagesList.get(lastCells.get(lastCells.size() - 1)).getBackground();
animation1.start();
animation2.start();
}
}
soundPool.play(sWrong, sVolume, sVolume, 1, 0, 1);
Ender ender = new Ender();
System.out.println("closing animation: line 900");
imagesList.get(lastCells.get(lastCells.size() - 2)).post(ender);
imagesList.get(lastCells.get(lastCells.size() - 1)).post(ender);
for (int i = 0; i < imagesList.size(); i++)
if (!openedSet.contains(i) && i != clicked)
imagesList.get(i).setEnabled(true);
wrongAnswered = false;
delta = 25;
delta_ = 0;
delta__ = 0;
timeAmount__ = 0;
displayingProcess = false;
choosingProcess = true;
resultProcess = false;
System.out.println("choosing: line 913");
} else if (correctAnswered) {
System.out.println("correct answered: line 915");
soundPool.play(sCorrect, sVolume, sVolume, 1, 0, 1);
correctAnswered = false;
if (finishedAll) {
delta = 0;
delta_ = 0;
delta__ = 0;
timeAmount__ = 0;
displayingProcess = false;
choosingProcess = false;
resultProcess = false;
Intent intent = new Intent(PairsGameActivity.this, PairsGameActivity.class);
intent.putExtra("currentLevel", currentLevel);
startActivity(intent);
finish();
} else {
delta = 25;
delta_ = 0;
delta__ = 0;
timeAmount__ = 0;
displayingProcess = false;
choosingProcess = true;
resultProcess = false;
System.out.println("choosing: line 933");
}
}
}
System.out.println("2. choosingProcess = " + choosingProcess);
System.out.println("2. resultProcess = " + resultProcess);
System.out.println("2. displayingProcess = " + displayingProcess);
System.out.println("2. timeAmount__ process = " + timeAmount__);
System.out.println("2. delta__ process = " + delta__);
}
});
}
这是输出:
I/System.out: 2. choosingProcess = false
I/System.out: 2. resultProcess = true
I/System.out: 2. displayingProcess = false
I/System.out: 2. timeAmount__ process = 525
I/System.out: 2. delta__ process = 25
E/process: 871
I/System.out: 2. choosingProcess = false
I/System.out: 2. resultProcess = true
I/System.out: 2. displayingProcess = false
I/System.out: 2. timeAmount__ process = 550
I/System.out: 2. delta__ process = 25
E/process: 872
I/System.out: 2. choosingProcess = false
I/System.out: 2. resultProcess = true
I/System.out: 2. displayingProcess = false
I/System.out: 2. timeAmount__ process = 575
I/System.out: 2. delta__ process = 25
E/process: 873
E/process: 874
I/System.out: 2. choosingProcess = false
I/System.out: 2. resultProcess = true
I/System.out: 2. displayingProcess = false
I/System.out: 2. timeAmount__ process = 625
I/System.out: 2. delta__ process = 25
I/System.out: 2. choosingProcess = false
I/System.out: 2. resultProcess = true
I/System.out: 2. displayingProcess = false
I/System.out: 2. timeAmount__ process = 625
I/System.out: 2. delta__ process = 25
E/process: 875
I/System.out: 2. choosingProcess = false
I/System.out: 2. resultProcess = true
I/System.out: 2. displayingProcess = false
I/System.out: 2. timeAmount__ process = 650
I/System.out: 2. delta__ process = 25
你可以在输出中看到奇怪的行为。
而不是输入if
循环(分配600),timeAmount__
在575之后两次分配值625并跳过循环。我怎么能避免这种行为?