I want that my program wait 10s in my while true, but it doesn't work
I tried to use Thread.sleep(10000);
but it isn't 10s
while (true) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
if (matrixVacancy[i][j] == 1) {
completeParking(i, j, R.color.vaga_ocupada);
} else {
completeParking(i, j, R.color.cor_chao);
}
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
}
int a, b, c, d, e, f, g, h, i;
a = (int) (Math.random() * 2); // indice i
b = (int) (Math.random() * 5); // indice j
c = (int) (Math.random() * 2); // tem ou nao carro
d = (int) (Math.random() * 2); // indice i
e = (int) (Math.random() * 5); // indice j
f = (int) (Math.random() * 2); // tem ou nao carro
g = (int) (Math.random() * 2); // indice i
h = (int) (Math.random() * 5); // indice j
i = (int) (Math.random() * 2); // tem ou nao carro
matrixVacancy[a][b] = c;
matrixVacancy[d][e] = f;
matrixVacancy[g][h] = i;
}
How can I do it? For my while wait 10s?
First I'd break point to see if your sleep is even called. Second I'd print the exception when your catching the InterruptException. Your sleep is correct so there's no reason it shouldn't be working so either someone is interrupting you or your not even getting to the sleep function.
Change:
to:
Check logcat to make sure that the sleep isn't being interrupted.
Also, put some Log statements in before and after your call to Thread.sleep printing out the elapsed time.
Logcat is your friend. :)
Depends what thread your trying to sleep. You can also put your method in a seperate thread and do your methods there. This way your app will not hang/sleep
To run this operation use