[英]how to check if thread is running or waiting in android
我有一個線程,我想知道wait方法何時運行。 當線程正在運行時
這是我的代碼:
runnableClass rc = new runnableClass();
Object PauseTHR = new Object();
boolean pause = false;
public class runnableClass implements Runnable {
@Override
public void run() {
while(true) {
// here is my code,after my code i want to wait thread
// so i call onpause();
onpause();
synchronized (PauseTHR) {
while(pause) {
Log.i("while loop", "it goes over me");
try {
PauseTHR.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void onpause() {
synchronized (PauseTHR) {
Log.i("synchronized", "it goes over me");
pause = true;
}
}
public void onresume() {
synchronized (PauseTHR) {
pause = false;
PauseTHR.notifyAll();
}
}
}
我將boolean(pause)變量設置為全局變量,但是當我檢查時
if(pause == true)
rc.onresume();
從我的Runnable類中退出,這似乎不起作用,我的條件正常了,我如何檢查線程何時等待以及線程何時runnig?
通過以下代碼檢查您的線程是否存在:
thread1.isAlive()
它返回一個布爾值。 如果接收器已經啟動並且仍在運行代碼(尚未終止),則返回true。 如果尚未啟動接收器,或者如果它已經啟動並運行到完成並死亡,則返回false。
使用Thread.getState()方法
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.State.html
線程可以處於以下狀態之一:
新,可運行,已阻止,正在等待,TIMED_WAITING,已終止
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.