gpt4 book ai didi

java - 从多个线程修改 ArrayAdapter 时如何防止 ConcurrentModificationException?

转载 作者:行者123 更新时间:2023-11-29 23:06:16 29 4
gpt4 key购买 nike

我有一个包含文件列表的 RecyclerView。用户可以点击多个 RecyclerView 行,然后上传所选文件。上传文件后(在后台线程上),我想从 RecyclerView 中删除该行。

但是,我收到错误:

java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at UploadActivity$11$1.onFinish(UploadActivity.java:471)
at FileUploader.upload(FileUploader.java:115)
at UploadActivity$11.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();
}

最佳答案

与其创建多个线程,不如创建一个线程并在其中一个接一个地上传所有文件。

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();
}

关于java - 从多个线程修改 ArrayAdapter 时如何防止 ConcurrentModificationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56479394/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com