3

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?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Douglas Fornaro
  • 2,017
  • 2
  • 22
  • 30
  • 2
    When exactly do you want your function to wait? Some more context please. – stealthjong Dec 07 '12 at 14:30
  • Exactly how you did it. How come it doesn't work? If your app stops responding for a while, Android will offer you to force-close it. Maybe you should run that loop on a separate (non-UI) thread? – nullpotent Dec 07 '12 at 14:30
  • 1
    Try to use CountDownTimer or simple Timer for this https://developer.android.com/reference/android/os/CountDownTimer.html https://developer.android.com/reference/java/util/Timer.html – Dmitriy Tarasov Dec 07 '12 at 14:32
  • Or use the [`Handler`](http://developer.android.com/reference/android/os/Handler.html) [`postDelayed`](http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29) method. But we really need some more context to know why you want to wait. – dave.c Dec 07 '12 at 15:00
  • in my function completeParking I setBackgorund in my LinearLayout, but with this while true, my program doens't exhibit nothing, the program shows a white screen and doens't show my LinearLayout – Douglas Fornaro Dec 07 '12 at 15:03
  • You still haven't explained why you want to wait. What is your program waiting for? What is the user experience or requirement that you are trying to meet? – dave.c Dec 07 '12 at 16:04

3 Answers3

15

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

private class TimeoutOperation extends AsyncTask<String, Void, Void>{

    @Override
    protected Void doInBackground(String... params) {

        try {
            Log.i(TAG, "Going to sleep");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "This is executed after 10 seconds and runs on the main thread");
        //Update your layout here
        super.onPostExecute(result);
    }
}

To run this operation use

new TimeoutOperation().execute("");
Oritm
  • 2,113
  • 2
  • 25
  • 40
  • I cant do it in other thread because my function completeParking set background in LinearLayout and to do this I need to do in original thread – Douglas Fornaro Dec 07 '12 at 15:00
  • The override method onPostExecute(Void result) runs on the main/UI thread! :) If you paste this code inside your class of the original thread, you can access the linear layout. To run the operation you should use: new TimeoutOperation().execute(""); – Oritm Dec 07 '12 at 15:28
1

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.

d3n13d1
  • 200
  • 12
1

Change:

    catch (InterruptedException ex) {
    }

to:

    catch (InterruptedException ex) {
        ex.printStackTrace();
    }

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. :)

Slynk
  • 527
  • 5
  • 9