應用程序的 AlertDialog.Builder(context)
中有下面的代碼。問題是在哪個線程中運行呢?
final Thread myPrettyOperation = new Thread() {
@Override
public void run() {
//Do some really long operation.
}
};
class MyPrettyTask extends
AsyncTask<Void, Integer, Boolean> {
protected Boolean doInBackground(
Void... voids) {
myPrettyOperation.start();
return true;
}
protected void onProgressUpdate(
Integer... progress) {
}
protected void onPostExecute(Boolean result) {
}
}
new MyPrettyTask().execute();
運行應用程序,然後檢查 logcat 輸出。線程名分別是 "Thread1:", "Thread2:", "Thread3:",當你在輸出中看見 "main" ,就是指你的 UI 線程。在其它情況下,你會看見線程的名字,例如: 是 myPrettyOperation 裡是"Thread14"。
final Thread myPrettyOperation = new Thread() {
@Override
public void run() {
Log.i("YOUR_TAG", "Thread1: "+ Thread.currentThread().getName());
//Do some really long operation.
}
};
class MyPrettyTask extends
AsyncTask<Void, Integer, Boolean> {
protected Boolean doInBackground(
Void... voids) {
Log.i("YOUR_TAG", "Thread2: "+ Thread.currentThread().getName());
myPrettyOperation.start();
return true;
}
protected void onProgressUpdate(
Integer... progress) {
}
protected void onPostExecute(Boolean result) {
}
}
new MyPrettyTask().execute();
Log.i("YOUR_TAG", "Thread3: "+ Thread.currentThread().getName());