在UI中,为了执行一些后台工作,我使用了一个单独的Thread.但正如其他人所说,我现在正在使用AsyncTask.
a Thread和an 之间的主要区别是AsyncTask什么?
在哪种情况下,我应该使用a Thread还是AsyncTask?
我相信Google建议开发人员使用AsyncTask.但是,我想知道它与使用"新线程"然后在性能和内存效率方面调用"RunOnUiThread"有何不同.
使用RunOnUithread的示例:
// some code #1
Thread t = new Thread("Thread1") {
@Override
public void run() {
// some code #2
runOnUiThread(new Runnable() {
public void run() {
// some code #3 (that needs to be ran in UI thread)
}
});
}
};
t.start();
Run Code Online (Sandbox Code Playgroud)
与
的AsyncTask:
onPreExecute() {
// some code #1
}
doInBackground() {
// some code #2
}
onPostExecute() {
// some code #3
}
Run Code Online (Sandbox Code Playgroud)
有什么优点/缺点?
编辑:
我不是在寻找像'更容易看到代码','方便开发人员'等答案.我实际上是在寻找幕后的技术差异.
例如,Paul Nikonowicz在下面的回答就是我想要的答案.(但AsyncTask的行为相同)