How to “wait” a Thread in Android

前端 未结 5 1781
夕颜
夕颜 2020-12-30 18:18
private void startGameTimeElapseThread(){
    new Thread(new Runnable() {
        Date d = new Date();
        public void run() {
            while (gameOn){
               


        
相关标签:
5条回答
  • 2020-12-30 19:01

    Don't use wait(), use either android.os.SystemClock.sleep(1000); or Thread.sleep(1000);.

    The main difference between them is that Thread.sleep() can be interrupted early -- you'll be told, but it's still not the full second. The android.os call will not wake early.

    0 讨论(0)
  • 2020-12-30 19:01

    I just add this line exactly as it appears below (if you need a second delay):

    try {
        Thread.sleep(1000);
    } catch(InterruptedException e) {
        // Process exception
    }
    

    I find the catch IS necessary (Your app can crash due to Android OS as much as your own code).

    0 讨论(0)
  • 2020-12-30 19:01

    You can try this one it is short :)

    SystemClock.sleep(7000);
    

    It will sleep for 7 sec look at documentation

    0 讨论(0)
  • 2020-12-30 19:08

    Write Thread.sleep(1000); it will make the thread sleep for 1000ms

    0 讨论(0)
  • 2020-12-30 19:20

    You need the sleep method of the Thread class.

    public static void sleep (long time)

    Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.

    Parameters

    time The time to sleep in milliseconds.

    0 讨论(0)
提交回复
热议问题