AsynckTask.onPostExecute和FragmentActivity.onResumeFragments是否在同一个线程(ui线程)上运行?

时间:2015-11-10 06:13:15

标签: android multithreading arraylist android-asynctask ui-thread

当我遍历ConcurrentModificationException时,我得到ArrayList。 我做了什么:

1.  activity has a ArrayList<Callable>
2.  add Callable to ArrayList when AsyncTask.onPostExecute
3.  traverse the ArrayList to call the callable in onResumeFragments

如何触发异常: 我制作了AsyncTask.execute()并快速按下主页按钮,当我的应用程序返回前台时,发生了异常。

令我困惑的是: 如果AsyncTask.onPostExecuteFragmentActivity.onResumeFragments在同一个帖子上运行, 那不是java.util.ConcurrentModificationException,对吧?

  

据我所知,AsyncTask.onPostExecute在ui线程上运行,FragmentActivity.onResumeFragments也应在ui线程上运行,   那么java.util.ConcurrentModificationException如何发生?

这是我的异常堆栈

com.yumei.sdkdemo I/PayMethodActivity: 
  11-10 11:34:35.218 10030-10030/com.yumei.sdkdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yumei.sdkdemo, PID: 10030
java.lang.RuntimeException: Unable to resume activity {com.yumei.sdkdemo/com.yumei.sdk.PayMethodActivity}: java.util.ConcurrentModificationException
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2954)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5235)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)
 Caused by: java.util.ConcurrentModificationException
 at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
 at com.yumei.sdk.PayMethodFragmentActivity.onResumeFragments(PayMethodActivity.java:116)
 at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:451)
 at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:141)
 at android.app.Activity.performResume(Activity.java:6040)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2943)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:135) 
 at android.app.ActivityThread.main(ActivityThread.java:5235) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)  

感谢您的回复。

2 个答案:

答案 0 :(得分:1)

请注意,如果您想在遍历时删除某些项目,那么您应该使用Iterator。如果你的AsynTask没有在UI线程上创建,请确保使用线程安全。

答案 1 :(得分:1)

是的,AsyncTask的onPostexcute是在UI线程中运行的,我不认为它是线程问题。 ConcurrentModificationException当您尝试modify Arraylist同时循环其元素(例如在Callable循环中移除for)时,可能会发生onPostExcute
p / s:你应该用你的logcat发布你的matrix代码。