AsyncTask#onPreExecute竟然在子线程执行?

zhujunhua2012 2014-11-19 10:44:11
加精
AsyncTask#onPreExecute,API说明是"Runs on the UI thread before doInBackground.",如果是在子线程中启动了此AsyncTask,log显示onPreExecute是跑在子线程里的(线程id和启动的子线程id一样),大神能给解释下?

new AsyncTask<Void, Void, Void>(){
@Override
protected void onPreExecute() {
android.util.Log.e("tag", "#onPreExecute, " + Thread.currentThread().getId());
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
android.util.Log.e("tag", "#doInBackground, " + Thread.currentThread().getId());
}
@Override
protected void onPostExecute(Void result) {
android.util.Log.e("tag", "#onPostExecute, " + Thread.currentThread().getId());
super.onPostExecute(result);
}
}.execute((Void)null);
...全文
2545 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujunhua2012 2015-09-18
  • 打赏
  • 举报
回复
#onPreExecute 是在 #execute里面调用的, 所以和方法调用处在同一个线程.
  • 打赏
  • 举报
回复
CodeC 2014-11-22
  • 打赏
  • 举报
回复
不得违反国家法律法规
heaven_wsh 2014-11-19
  • 打赏
  • 举报
回复
引用 3 楼 zhujunhua_2010 的回复:
多谢,知道原因了,但是onPreExecute这个方法的API 说明有点不准确啊。。。 [quote=引用 2 楼 tiantang198707 的回复:] 你放在子线程里启动是错误的,AsyncTask是辅助用来后台执行,然后更新UI的,只能在UI线程下启动 Threading rules There are a few threading rules that must be followed for this class to work properly: The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN. The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. http://developer.android.com/intl/zh-cn/reference/android/os/AsyncTask.html
[/quote] 人家有前提要在UI线程下启动和创建,在这个前提下,文档描述是没有问题的
zhujunhua2012 2014-11-19
  • 打赏
  • 举报
回复
多谢,知道原因了,但是onPreExecute这个方法的API 说明有点不准确啊。。。
引用 2 楼 tiantang198707 的回复:
你放在子线程里启动是错误的,AsyncTask是辅助用来后台执行,然后更新UI的,只能在UI线程下启动 Threading rules There are a few threading rules that must be followed for this class to work properly: The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN. The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. http://developer.android.com/intl/zh-cn/reference/android/os/AsyncTask.html
heaven_wsh 2014-11-19
  • 打赏
  • 举报
回复
你放在子线程里启动是错误的,AsyncTask是辅助用来后台执行,然后更新UI的,只能在UI线程下启动 Threading rules There are a few threading rules that must be followed for this class to work properly: The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN. The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. http://developer.android.com/intl/zh-cn/reference/android/os/AsyncTask.html
Birds2018 2014-11-19
  • 打赏
  • 举报
回复
AsyncTask 这个里面启动了一个线程,如果把这个东西再放到一个线程里面 就等于2个线程了。 线程id就不一样了
技术小黑屋 2014-11-19
  • 打赏
  • 举报
回复
应该是创建AsyncTask并启动没有在主线程中,只有这样才能对后续的onPostExecute()对UI进行操作。如需了解更多地关于AsyncTask,可以看看这篇文章http://droidyue.com/blog/2014/11/08/bad-smell-of-asynctask-in-android/

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧