我正在Android Juint中實現一個測試用例。我在 處理線程時遇到問題。測試用例成功運行後,不會等待子線程完成。例如如果一個測試用例在服務器中調用 一些服務。 testcase將成功發送請求到 服務器,但它不會等待響應。因爲測試用例會在 的不同線程中啓動此請求。目前我使用 下面的代碼,Android Junit測試用例中的線程處理問題
currentThread = Thread.currentThread();
synchronized (Thread.currentThread()) {
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
得到響應我使用下面的代碼再次啓動 當前線程之後。
currentThread.interrupt();
我認爲這不是好的做法。必須有一些其他的技術來正確處理這個問題。如果有人知道解決方案,請儘快告訴我 。
感謝,
Vashishta
如果你想等待線程完成,爲什麼不使用Thread.join()? – fadden 2010-11-12 22:35:22