2011-05-27 36 views
40
private void startGameTimeElapseThread(){ 
    new Thread(new Runnable() { 
     Date d = new Date(); 
     public void run() { 
      while (gameOn){ 
       Log.d(TAG,""+d.getTime()+" "+gameStatus.getLastTimeUpdated()); 

       postInvalidate(795, 150, 1024, 300); 
       try { 
        synchronized (this) { 
         wait(1000); 
        } 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        Log.d(TAG, "Waiting didnt work!!"); 
        e.printStackTrace(); 
       } 
      } 
     } 
    }).start(); 
} 

這裏我想每秒調用一次「Log.d」和「postInvalidate」。但是當我從LogCat中檢查它看來循環運行速度比我想要的要快。爲什麼這個循環只是等待1000毫秒?如何「等待」Android中的線程

以下是LogCat中的輸出。所以你可以看到它不睡一秒鐘。我也使用Thread.sleep(建議後)

05-27 11:40:10.060: DEBUG/NOPSA-P(4546): 1306496408950 1306496387535 
05-27 11:40:10.070: DEBUG/NOPSA-P(4546): 1306496410037 1306496387535 
05-27 11:40:10.232: DEBUG/NOPSA-P(4546): 1306496410201 1306496387535 
05-27 11:40:10.250: DEBUG/NOPSA-P(4546): 1306496408214 1306496387535 
05-27 11:40:10.310: DEBUG/NOPSA-P(4546): 1306496405988 1306496387535 
05-27 11:40:10.361: DEBUG/NOPSA-P(4546): 1306496409327 1306496387535 
05-27 11:40:10.431: DEBUG/NOPSA-P(4546): 1306496410398 1306496387535 
05-27 11:40:10.521: DEBUG/NOPSA-P(4546): 1306496409478 1306496387535 
05-27 11:40:10.593: DEBUG/NOPSA-P(4546): 1306496410566 1306496387535 
05-27 11:40:11.230: DEBUG/NOPSA-P(4546): 1306496409678 1306496387535 
05-27 11:40:11.230: DEBUG/NOPSA-P(4546): 1306496410201 1306496387535 
05-27 11:40:11.241: DEBUG/NOPSA-P(4546): 1306496409878 1306496387535 
05-27 11:40:11.241: DEBUG/NOPSA-P(4546): 1306496410037 1306496387535 
05-27 11:40:11.241: DEBUG/NOPSA-P(4546): 1306496408950 1306496387535 
05-27 11:40:11.260: DEBUG/NOPSA-P(4546): 1306496408214 1306496387535 
05-27 11:40:11.314: DEBUG/NOPSA-P(4546): 1306496405988 1306496387535 
05-27 11:40:11.314: DEBUG/NOPSA-P(4546): 1306496411254 1306496387535 
05-27 11:40:11.361: DEBUG/NOPSA-P(4546): 1306496409327 1306496387535 
05-27 11:40:11.431: DEBUG/NOPSA-P(4546): 1306496410398 1306496387535 
05-27 11:40:11.520: DEBUG/NOPSA-P(4546): 1306496409478 1306496387535 
05-27 11:40:11.551: DEBUG/NOPSA-P(4546): 1306496411508 1306496387535 
05-27 11:40:11.712: DEBUG/NOPSA-P(4546): 1306496410566 1306496387535 
05-27 11:40:11.881: DEBUG/NOPSA-P(4546): 1306496411865 1306496387535 
05-27 11:40:12.051: DEBUG/NOPSA-P(4546): 1306496412025 1306496387535 
05-27 11:40:12.211: DEBUG/NOPSA-P(4546): 1306496412198 1306496387535 
05-27 11:40:12.231: DEBUG/NOPSA-P(4546): 1306496409678 1306496387535 
05-27 11:40:12.242: DEBUG/NOPSA-P(4546): 1306496410201 1306496387535 
05-27 11:40:12.251: DEBUG/NOPSA-P(4546): 1306496408950 1306496387535 
05-27 11:40:12.251: DEBUG/NOPSA-P(4546): 1306496409878 1306496387535 
05-27 11:40:12.251: DEBUG/NOPSA-P(4546): 1306496410037 1306496387535 
05-27 11:40:12.261: DEBUG/NOPSA-P(4546): 1306496408214 1306496387535 
05-27 11:40:12.311: DEBUG/NOPSA-P(4546): 1306496405988 1306496387535 
05-27 11:40:12.341: DEBUG/NOPSA-P(4546): 1306496411254 1306496387535 
05-27 11:40:12.371: DEBUG/NOPSA-P(4546): 1306496409327 1306496387535 
05-27 11:40:12.442: DEBUG/NOPSA-P(4546): 1306496410398 1306496387535 
05-27 11:40:12.471: DEBUG/NOPSA-P(4546): 1306496412431 1306496387535 
05-27 11:40:12.521: DEBUG/NOPSA-P(4546): 1306496409478 1306496387535 
05-27 11:40:12.553: DEBUG/NOPSA-P(4546): 1306496411508 1306496387535 
05-27 11:40:12.671: DEBUG/NOPSA-P(4546): 1306496412639 1306496387535 
05-27 11:40:12.711: DEBUG/NOPSA-P(4546): 1306496410566 1306496387535 
05-27 11:40:12.841: DEBUG/NOPSA-P(4546): 1306496412813 1306496387535 
05-27 11:40:12.891: DEBUG/NOPSA-P(4546): 1306496411865 1306496387535 
05-27 11:40:13.001: DEBUG/NOPSA-P(4546): 1306496412983 1306496387535 
05-27 11:40:13.051: DEBUG/NOPSA-P(4546): 1306496412025 1306496387535 
05-27 11:40:13.193: DEBUG/NOPSA-P(4546): 1306496413166 1306496387535 
05-27 11:40:13.231: DEBUG/NOPSA-P(4546): 1306496409678 1306496387535 
05-27 11:40:13.251: DEBUG/NOPSA-P(4546): 1306496410201 1306496387535 
05-27 11:40:13.251: DEBUG/NOPSA-P(4546): 1306496409878 1306496387535 
05-27 11:40:13.251: DEBUG/NOPSA-P(4546): 1306496410037 1306496387535 
05-27 11:40:13.251: DEBUG/NOPSA-P(4546): 1306496408950 1306496387535 
05-27 11:40:13.261: DEBUG/NOPSA-P(4546): 1306496408214 1306496387535 
05-27 11:40:13.271: DEBUG/NOPSA-P(4546): 1306496412198 1306496387535 
05-27 11:40:13.321: DEBUG/NOPSA-P(4546): 1306496405988 1306496387535 
05-27 11:40:13.341: DEBUG/NOPSA-P(4546): 1306496411254 1306496387535 
05-27 11:40:13.371: DEBUG/NOPSA-P(4546): 1306496409327 1306496387535 
05-27 11:40:13.431: DEBUG/NOPSA-P(4546): 1306496413405 1306496387535 
05-27 11:40:13.441: DEBUG/NOPSA-P(4546): 1306496410398 1306496387535 
05-27 11:40:13.481: DEBUG/NOPSA-P(4546): 1306496412431 1306496387535 
05-27 11:40:13.532: DEBUG/NOPSA-P(4546): 1306496409478 1306496387535 

這是最新的代碼。 gameOn是一個布爾值,現在它是真的。

private void startGameTimeElapseThread(){ 
    if (gameOn){ 
     new Thread(new Runnable() { 
      Date d = new Date(); 
      public void run() { 

        Log.d(TAG,""+d.getTime()+"ms "+d.getTime()/1000+"sec"); 
        postInvalidate(795, 150, 1024, 300); 
        android.os.SystemClock.sleep(1000); 
        startGameTimeElapseThread(); 

      } 
     }).start(); 
    } 
} 

輸出是

05-27 12:00:51.211: DEBUG/NOPSA-P(5098): 1306497650761ms 1306497650sec 
05-27 12:00:51.211: DEBUG/NOPSA-P(5098): 1306497650786ms 1306497650sec 
05-27 12:00:51.221: DEBUG/NOPSA-P(5098): 1306497650764ms 1306497650sec 
05-27 12:00:51.221: DEBUG/NOPSA-P(5098): 1306497650763ms 1306497650sec 
05-27 12:00:51.221: DEBUG/NOPSA-P(5098): 1306497650769ms 1306497650sec 
05-27 12:00:51.231: DEBUG/NOPSA-P(5098): 1306497650749ms 1306497650sec 
05-27 12:00:51.231: DEBUG/NOPSA-P(5098): 1306497650736ms 1306497650sec 
05-27 12:00:51.231: DEBUG/NOPSA-P(5098): 1306497650742ms 1306497650sec 
05-27 12:00:51.241: DEBUG/NOPSA-P(5098): 1306497650746ms 1306497650sec 
05-27 12:00:51.241: DEBUG/NOPSA-P(5098): 1306497650848ms 1306497650sec 
05-27 12:00:51.251: DEBUG/NOPSA-P(5098): 1306497650729ms 1306497650sec 
05-27 12:00:51.271: DEBUG/NOPSA-P(5098): 1306497650739ms 1306497650sec 
05-27 12:00:51.271: DEBUG/NOPSA-P(5098): 1306497650739ms 1306497650sec 
05-27 12:00:51.271: DEBUG/NOPSA-P(5098): 1306497650868ms 1306497650sec 
05-27 12:00:51.271: DEBUG/NOPSA-P(5098): 1306497650798ms 1306497650sec 
05-27 12:00:51.281: DEBUG/NOPSA-P(5098): 1306497650870ms 1306497650sec 
05-27 12:00:51.291: DEBUG/NOPSA-P(5098): 1306497651020ms 1306497651sec 
05-27 12:00:51.551: DEBUG/NOPSA-P(5098): 1306497651498ms 1306497651sec 
05-27 12:00:51.551: DEBUG/NOPSA-P(5098): 1306497651502ms 1306497651sec 
05-27 12:00:51.642: DEBUG/NOPSA-P(5098): 1306497651523ms 1306497651sec 
05-27 12:00:51.651: DEBUG/NOPSA-P(5098): 1306497651537ms 1306497651sec 
05-27 12:00:51.691: DEBUG/NOPSA-P(5098): 1306497651529ms 1306497651sec 
05-27 12:00:51.701: DEBUG/NOPSA-P(5098): 1306497651534ms 1306497651sec 
05-27 12:00:51.784: DEBUG/NOPSA-P(5098): 1306497651527ms 1306497651sec 
05-27 12:00:51.801: DEBUG/NOPSA-P(5098): 1306497651570ms 1306497651sec 
05-27 12:00:51.801: DEBUG/NOPSA-P(5098): 1306497651564ms 1306497651sec 
05-27 12:00:51.891: DEBUG/NOPSA-P(5098): 1306497651682ms 1306497651sec 
05-27 12:00:51.891: DEBUG/NOPSA-P(5098): 1306497651690ms 1306497651sec 
05-27 12:00:51.891: DEBUG/NOPSA-P(5098): 1306497651685ms 1306497651sec 
05-27 12:00:51.901: DEBUG/NOPSA-P(5098): 1306497651693ms 1306497651sec 
05-27 12:00:51.914: DEBUG/NOPSA-P(5098): 1306497651647ms 1306497651sec 
05-27 12:00:51.914: DEBUG/NOPSA-P(5098): 1306497651667ms 1306497651sec 
05-27 12:00:51.921: DEBUG/NOPSA-P(5098): 1306497651666ms 1306497651sec 
05-27 12:00:51.921: DEBUG/NOPSA-P(5098): 1306497651658ms 1306497651sec 
05-27 12:00:51.921: DEBUG/NOPSA-P(5098): 1306497651663ms 1306497651sec 
05-27 12:00:51.932: DEBUG/NOPSA-P(5098): 1306497651688ms 1306497651sec 
05-27 12:00:51.941: DEBUG/NOPSA-P(5098): 1306497651711ms 1306497651sec 
05-27 12:00:51.961: DEBUG/NOPSA-P(5098): 1306497651792ms 1306497651sec 
05-27 12:00:51.971: DEBUG/NOPSA-P(5098): 1306497651629ms 1306497651sec 
05-27 12:00:51.981: DEBUG/NOPSA-P(5098): 1306497651714ms 1306497651sec 
05-27 12:00:52.083: DEBUG/NOPSA-P(5098): 1306497652048ms 1306497652sec 

回答

37

您需要Thread類的sleep方法。

public static void sleep (long time)

導致其中發送此消息睡給定的時間間隔(以毫秒爲單位)的線程。不能保證精度 - 線程可能會比請求的多或少睡眠。

參數

time以毫秒爲單位睡覺的時間。

+0

我加了一些輸出,用於我的代碼。您可以請檢查一下:) – dinesh707 2011-05-27 11:43:36

+1

@ dinesh707:您多次調用'startGameTimeElapseThread()'方法,每次調用都會創建一個線程。只是打電話一次,看看。 – 2011-05-27 11:57:47

+1

@ dinesh707:可能會記錄'Thread.currentThread()。getName()'輸出並驗證自己。 – 2011-05-27 11:59:22

37

請勿使用wait(),請使用android.os.SystemClock.sleep(1000);Thread.sleep(1000);。它們之間的主要區別是Thread.sleep()可以被提前中斷 - 你會被告知,但它仍然不是整秒。 android.os調用不會提前醒來。

+1

我爲我的代碼添加了一些輸出。你可以請檢查一下:) – dinesh707 2011-05-27 11:43:30

+0

輸出不與你的日誌語句,因爲第一列既不固定(因爲我認爲它應該是,因爲你的代碼剪切 - D從來沒有更新),也不嚴格越來越多。左邊的時間戳雖然與你同意 - 你還沒睡一秒鐘。你能在這篇文章中更新你的代碼嗎? – mah 2011-05-27 11:54:45

+0

我更新了最新的代碼。 – dinesh707 2011-05-27 12:03:45

1

Thread.sleep(1000);它會爲1000毫秒

18

線程睡眠我剛加入這一行正是因爲它顯示如下(如果你需要一秒鐘的延遲):

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

我覺得美中不足的是必要的。

6

你可以試試這個一個很短:)

​​

它會睡眠7秒 看看documentation