我创建新线程并启动它。它的运行方法有以下代码:
for (int a=0; a<10; a++) {
System.out.println(a);
Thread.sleep(10);
}
我得到的是:
0
0
1
1
2
0
2
3
1
0
3
4
2
1
0
4
5
3
2
1
0
5
6
4
... 等等。为什么我不明白1 2 3 4 5 6 7 8 9
?什么原因?
编辑:
线程代码:
private class WarpEnemyRnn implements Runnable {
private WarpEnemy enemy;
public WarpEnemyRnn(WarpEnemy enemy) {
this.enemy = enemy;
}
@Override
public void run() {
try {
for (int a=0; a<8; a++) {
System.out.println(a);
enemy.subOpacity();
Thread.sleep(refreshRate);
}
Point2D warpPoint = enemy.warp();
enemy.setX((int) warpPoint.getX());
enemy.setY((int) warpPoint.getY());
enemy.resetWarpCooldown();
for (int a=0; a<8; a++) {
enemy.addOpacity();
Thread.sleep(refreshRate);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
新线程在其他线程中创建,每 15 毫秒运行一次。只有在某些条件一致时才会创建它。