【发布时间】:2015-11-04 20:24:56
【问题描述】:
我正在研究一个大型应用程序的代码。 UI 中使用了 3 种异步模式,它们看起来都一样:
模式 1,异步任务
new AsyncTask<X, Void, Z>() {
protected Boolean doInBackground(X... params) {
//background task
}
protected void onPostExecute(Z res) {
//UI callback
}
}.execute();
模式 2,Activity.runOnUiThread(Runnable)
new Thread() {
public void run() {
//background task
runOnUiThread(new Runnable() {
public void run() {
//UI callback
}
});
}
}.start();
模式 3,Handler.post(Runnable)
new Thread() {
public void run() {
//background task
handler.post(new Runnable() {
public void run() {
//UI callback
}
});
}
}.start();
问题:
- 我缺少的 3 种模式之间有什么区别吗? (除了 AsyncTask 在具有后台优先级的预先存在的线程池上运行。)
- 在任何情况下都会首选特定模式?
【问题讨论】:
标签: android asynchronous android-asynctask