案例1:这不会导致ConcurrentModificationException
?谁能告诉我为什么这不会导致 ConcurrentModificationException。
public class UpdatePeople {
List < People > records = new ArrayList < People > ();
public class AsyncTask extends AsyncTask < Void, Void, Boolean > {
List < People > people;
public AsyncTask(List < People > allergy) {
this.people = people;
}@
Override
protected Boolean doInBackground(Void...params) {
List < String > responses = new ArrayList < String > ();
for (People peopleList: this.people) {
}
}
}
}
案例 2:这导致ConcurrentModificationException
我试图访问我的 AsyncThread 中的人员列表,这不是线程安全的。我可以让我的人员列表实现CopyOnWriteArrayList
它是线程安全的,这应该可以工作。
public class UpdatePeople {
List < People > records = new ArrayList < People > ();
public class AsyncTask extends AsyncTask < Void, Void, Boolean > {
@
Override
protected Boolean doInBackground(Void...params) {
List < String > responses = new ArrayList < String > ();
for (People peopleList: records) {
}
}
}
}
- 谁能解释我到底发生了什么
case 1
。我无法理解这是如何解决ConcurrentModificationException
问题的。 - 案例 2 是否将实施从推荐
ArrayList
改为CopyOnWriteArrayList
推荐?
添加异常:
05-28 20:34:21.073: E/XXX(904): 未捕获的异常是: 05-28 20:34:21.073: E/XXX(904): java.lang.RuntimeException: 执行 doInBackground() 时出错05-28 20:34:21.073: E/XXX(904): 在 android.os.AsyncTask$3.done(AsyncTask.java:299) 05-28 20:34:21.073: E/XXX(904): 在 java .util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 05-28 20:34:21.073: E/XXX(904): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 05-28 20:34:21.073: E/XXX(904): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 05-28 20:34:21.073: E/XXX(904) : 在 java.util.concurrent.FutureTask.run(FutureTask.java:137) 05-28 20:34:21.073: E/XXX(904): 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java: 230) 05-28 20:34:21.073: E/XXX(904): 在 java.util.concurrent。ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 05-28 20:34:21.073: E/XXX(904): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 05-28 20: 34:21.073: E/XXX(904): at java.lang.Thread.run(Thread.java:856) 05-28 20:34:21.073: E/XXX(904): 由: java.util.ConcurrentModificationException 05-28 20:34:21.073: E/XXX(904): 在 java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)ArrayList$ArrayListIterator.next(ArrayList.java:569)ArrayList$ArrayListIterator.next(ArrayList.java:569)