从多个线程修改 ArrayAdapter 时如何防止 ConcurrentModificationException?
How to prevent ConcurrentModificationException when modifying an ArrayAdapter from multiple threads?
我有一个包含文件列表的 RecyclerView。用户可以点击多个 RecyclerView 行,然后上传所选文件。上传文件后(在后台线程上),我想从 RecyclerView 中删除该行。
但是,我收到错误消息:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at UploadActivity.onFinish(UploadActivity.java:471)
at FileUploader.upload(FileUploader.java:115)
at UploadActivity.run(UploadActivity.java:458)
我知道我有多个线程同时访问和修改我的 RecyclerView 适配器,但我不确定如何解决这个问题(我的代码和尝试在下面发布)。
首先我循环遍历选定的 RecyclerView 项目,获取每个选定的文件,然后调用 upload()
// Loop through all selected RecyclerView items
for (int i = 0; i < selectedIndicies.size(); i++) {
// Get the i-th selected item
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
// Get the file associated with the i-th selected item
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
// Upload the file
uploadFile(file);
}
然后我开始一个新线程,开始上传,并定义onFinish()回调
public void uploadFile(SaveFile saveFile) {
...
new Thread() {
@Override
public void run() {
//
// Uploads the given file, when the upload is complete
// the onFinish() method is called and the file is passed
// back so I can update the RecyclerView
//
FileUploader.upload(saveFile, new FileUploader.FileUploadListener() {
@Override
public void onFinish(SaveFile file) {
// Loop through all items in RecyclerView
for (Upload_Item_Model item : adapter.getFilteredData()) { // this is line 471 where the crash happens
//
// If the RecyclerView item has the same name
// as the returned file, then it is
// the file I just uploaded
//
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
// Removes the item from the adapter
adapter.removeItem(item);
adapter.notifyDataSetChanged();
});
}
}
}
});
}
}.start();
}
在适配器中,我有以下访问和修改适配器 ArrayList 的函数。我试图使这些线程安全但没有运气。
public ArrayList<Upload_Item_Model> getFilteredData() {
synchronized (this.filteredData) {
return this.filteredData;
}
}
public void removeItem(Upload_Item_Model item) {
synchronized (this.filteredData) {
this.filteredData.remove(item);
}
}
感谢任何帮助或建议!
编辑+解决方案
我最终使用 Rajat Mehra 的解决方案使一切正常工作,该解决方案使用单个线程上传所有文件,而不是使用多个线程仅上传一个文件。我确实需要做一些小的调整才能让它工作,但现在一切都很顺利。
public void uploadFile() {
new Thread() {
@Override
public void run() {
for (int i = 0; i < selectedIndicies.size(); i++) {
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
FileUploader.upload(file, new FileUploader.FileUploadListener() {
@Override
public void onFinish() {
runOnUiThread(() -> {
// I can now simply use the selectedItem here!
adapter.removeItem(selectedItem);
adapter.notifyDataSetChanged();
});
}
});
}
}
}.start();
}
当您调用 adapter.notifyDataSetChanged();
时会抛出异常,因为在后台 notifyDataSetChanged
对您的项目做了一些事情(没有同步),同时您正在调用 this.filteredData.remove(item);
并且它有或没有 synchronized 关键字都没有关系。
可能首先你需要调用所有 removeItem
然后调用 adapter.notifyDataSetChanged();
像这样:
for (Upload_Item_Model item: adapter.getFilteredData()) {
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
adapter.removeItem(item);
});
}
}
adapter.notifyDataSetChanged();
思考:notifyDataSetChanged()
不是更新回收站视图项目的有效方法。有一个 DiffUtil.Callback
,它检查两个数据集之间的差异并有效地将更改分派到回收器视图。
与其创建多个线程,不如创建一个线程并在其中一个一个地上传所有文件。
public void uploadFile() {
new Thread() {
@Override
public void run() {
//
// Uploads the given file, when the upload is complete
// the onFinish() method is called and the file is passed
// back so I can update the RecyclerView
//
for (int i = 0; i < selectedIndicies.size(); i++) {
// Get the i-th selected item
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
// Get the file associated with the i-th selected item
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
FileUploader.upload(file, new FileUploader.FileUploadListener() {
@Override
public void onFinish(SaveFile file) {
// Loop through all items in RecyclerView
for (Upload_Item_Model item : adapter.getFilteredData()) { // this is line 471 where the crash happens
//
// If the RecyclerView item has the same name
// as the returned file, then it is
// the file I just uploaded
//
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
// Removes the item from the adapter
adapter.removeItem(item);
adapter.notifyDataSetChanged();
});
}
}
}
});
}
}
}.start();
}
我有一个包含文件列表的 RecyclerView。用户可以点击多个 RecyclerView 行,然后上传所选文件。上传文件后(在后台线程上),我想从 RecyclerView 中删除该行。
但是,我收到错误消息:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at UploadActivity.onFinish(UploadActivity.java:471)
at FileUploader.upload(FileUploader.java:115)
at UploadActivity.run(UploadActivity.java:458)
我知道我有多个线程同时访问和修改我的 RecyclerView 适配器,但我不确定如何解决这个问题(我的代码和尝试在下面发布)。
首先我循环遍历选定的 RecyclerView 项目,获取每个选定的文件,然后调用 upload()
// Loop through all selected RecyclerView items
for (int i = 0; i < selectedIndicies.size(); i++) {
// Get the i-th selected item
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
// Get the file associated with the i-th selected item
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
// Upload the file
uploadFile(file);
}
然后我开始一个新线程,开始上传,并定义onFinish()回调
public void uploadFile(SaveFile saveFile) {
...
new Thread() {
@Override
public void run() {
//
// Uploads the given file, when the upload is complete
// the onFinish() method is called and the file is passed
// back so I can update the RecyclerView
//
FileUploader.upload(saveFile, new FileUploader.FileUploadListener() {
@Override
public void onFinish(SaveFile file) {
// Loop through all items in RecyclerView
for (Upload_Item_Model item : adapter.getFilteredData()) { // this is line 471 where the crash happens
//
// If the RecyclerView item has the same name
// as the returned file, then it is
// the file I just uploaded
//
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
// Removes the item from the adapter
adapter.removeItem(item);
adapter.notifyDataSetChanged();
});
}
}
}
});
}
}.start();
}
在适配器中,我有以下访问和修改适配器 ArrayList 的函数。我试图使这些线程安全但没有运气。
public ArrayList<Upload_Item_Model> getFilteredData() {
synchronized (this.filteredData) {
return this.filteredData;
}
}
public void removeItem(Upload_Item_Model item) {
synchronized (this.filteredData) {
this.filteredData.remove(item);
}
}
感谢任何帮助或建议!
编辑+解决方案
我最终使用 Rajat Mehra 的解决方案使一切正常工作,该解决方案使用单个线程上传所有文件,而不是使用多个线程仅上传一个文件。我确实需要做一些小的调整才能让它工作,但现在一切都很顺利。
public void uploadFile() {
new Thread() {
@Override
public void run() {
for (int i = 0; i < selectedIndicies.size(); i++) {
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
FileUploader.upload(file, new FileUploader.FileUploadListener() {
@Override
public void onFinish() {
runOnUiThread(() -> {
// I can now simply use the selectedItem here!
adapter.removeItem(selectedItem);
adapter.notifyDataSetChanged();
});
}
});
}
}
}.start();
}
当您调用 adapter.notifyDataSetChanged();
时会抛出异常,因为在后台 notifyDataSetChanged
对您的项目做了一些事情(没有同步),同时您正在调用 this.filteredData.remove(item);
并且它有或没有 synchronized 关键字都没有关系。
可能首先你需要调用所有 removeItem
然后调用 adapter.notifyDataSetChanged();
像这样:
for (Upload_Item_Model item: adapter.getFilteredData()) {
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
adapter.removeItem(item);
});
}
}
adapter.notifyDataSetChanged();
思考:notifyDataSetChanged()
不是更新回收站视图项目的有效方法。有一个 DiffUtil.Callback
,它检查两个数据集之间的差异并有效地将更改分派到回收器视图。
与其创建多个线程,不如创建一个线程并在其中一个一个地上传所有文件。
public void uploadFile() {
new Thread() {
@Override
public void run() {
//
// Uploads the given file, when the upload is complete
// the onFinish() method is called and the file is passed
// back so I can update the RecyclerView
//
for (int i = 0; i < selectedIndicies.size(); i++) {
// Get the i-th selected item
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
// Get the file associated with the i-th selected item
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
FileUploader.upload(file, new FileUploader.FileUploadListener() {
@Override
public void onFinish(SaveFile file) {
// Loop through all items in RecyclerView
for (Upload_Item_Model item : adapter.getFilteredData()) { // this is line 471 where the crash happens
//
// If the RecyclerView item has the same name
// as the returned file, then it is
// the file I just uploaded
//
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
// Removes the item from the adapter
adapter.removeItem(item);
adapter.notifyDataSetChanged();
});
}
}
}
});
}
}
}.start();
}