【问题标题】:JavaFX8: exception when adding items to ObservableList: ConcurrentModificationException [closed]JavaFX8:将项目添加到 ObservableList 时出现异常:ConcurrentModificationException [关闭]
【发布时间】:2015-05-23 17:24:37
【问题描述】:

我有一个列出月份值的选择框,当用户选择一个值时,它会执行这个 lambda 表达式:

private TableView<IncomeFX> tableIncome;
private ChoiceBox<Month> choiceBoxIncomeMonths;

private ChangeListener<Month> setChoiceBoxIncomeMonthsBehaviour(){
    ChangeListener<Month> months = (ObservableValue<? extends Month> observable, Month oldValue, Month newValue) -> {
            incomesData.clear();
            Year selectedYear = choiceBoxIncomeYears.getSelectionModel().getSelectedItem();
            ObservableList<IncomeFX> temp = incomeManager.getIncomesOf(selectedYear, newValue);
            incomesData.addAll(temp);

    };
    return months;
}

以及我如何添加监听器:

choiceBoxIncomeMonths.getSelectionModel().selectedItemProperty().addListener(setChoiceBoxIncomeMonthsBehaviour());

当我点击选择框时,我得到:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:386)
at java.util.AbstractList$Itr.next(AbstractList.java:355)
at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
at javafx.collections.ModifiableObservableListBase.addAll(ModifiableObservableListBase.java:99)
at lite.money.ui.MainUI.lambda$1(MainUI.java:160)
at lite.money.ui.MainUI$$Lambda$120/1680764266.changed(Unknown Source)

它表明问题出在我调用的行中:addAll(temp) 我该如何解决?谢谢

【问题讨论】:

标签: java javafx observablelist


【解决方案1】:

由于您尚未发布所有代码,我猜您正在尝试与 JavaFX 数据交互的另一个线程上运行代码。当另一个线程尝试这样做时,它会抛出异常,因为只有 JavaFX 线程应该与数据交互。

我真的无法提供更多建议,因为我没有完整的代码库来说明你正在做的事情真正说“是的,在第 X 行,你有线程 Y 访问位置 X,而它不应该是。”

你是在另一个线程上添加这个吗?你会比我更了解这个应用程序,因为我没有更多的代码可以使用。

【讨论】:

  • 很抱歉,但是您想要什么样的代码,我发布了复选框侦听器设置以及当ChoiceBox的值更改时执行的lambda表达式包含要在表格视图中显示的类。该表已经填充了一些值,这就是为什么我先清除它然后添加新值。没有其他线程只有主线程。
  • ConcurrentModificationException 表示您正在同时修改不应该修改的内容。因此,您要么正在使用多个线程(可能不知道),要么正在发生其他一些错误(我猜这不太可能)。因此,如果您的应用程序没有更多代码来查看您是否正在执行多个线程,我们真的无法进一步建议您。
  • 如果你不相信我的话:docs.oracle.com/javase/8/docs/api/java/util/…
  • 没有人我相信你,只是我不知道要添加什么其他代码,因为有很多 UI 代码,有逻辑,还有 db 代码,都与选择框更改了值。
  • @usertest 我有个主意,你能在它打印出所有线程之前转到导致错误的行吗?它可能看起来像这样:Set&lt;Thread&gt; threadSet = Thread.getAllStackTraces().keySet(); Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]); for (Thread t : threadArray) { if (t.isAlive() &amp;&amp; !t.isDaemon()) { System.out.println(t); } } -- 抱歉,这看起来像废话,但我不想将其作为答案发布,因为我并没有真正回答任何问题。这样我们就可以查看是否还有其他线程。 JavaFX 确实有自己的线程。
【解决方案2】:

这是我解决它的方法,我知道这是一个糟糕的代码,但我不知道任何其他解决方案,我必须清除它两次,否则如果我没有清除它,项目将被添加,如果你有其他解决方案我会很高兴:

private ChangeListener<Month> setChoiceBoxIncomeMonthsBehaviour(){
    ChangeListener<Month> months = (ObservableValue<? extends Month> observable, Month oldValue, Month newValue) -> {
        if (!lastMonthValuesFired) {
            incomesData.clear();
            Year selectedYear = choiceBoxIncomeYears.getSelectionModel().getSelectedItem();
            ObservableList<IncomeFX> temp = incomeManager.getIncomesOf(selectedYear, newValue);
            ObservableList<IncomeFX> temp2 = FXCollections.observableList(new ArrayList<IncomeFX>());

            for (IncomeFX t : temp) {
                temp2.add(t);
            }
            incomesData.clear();
            incomesData.addAll(temp2);
        }
    };
    return months;
}

【讨论】:

    【解决方案3】:

    我遇到了同样的问题并做了一些研究,这是我发现对我有用的解决方案:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    
    /**
     * Java Program to demonstrate how to deal with
     * ConcurrentModificationException.
     * Unlike the name suggests, this error can come even if only 
     * one thread is modifying the collection e.g. List. 
     * It happens when you modify collection 
     * while iterating over it e.g. adding new element or removing elements.
     * 
     * If you want to remove elements while traversing list then 
     * make sure you use Iterator's remove() method or not ArrayList's remove()
     * method() to avoid ConcurrentModificationExcetpion.
     * 
     * @author WINDOWS 8
     *
     */
    public class ConcurrentModExceptionDemo{
    
        public static void main(String args[]) {
    
            List<String> listOfPhones = new ArrayList<String>(Arrays.asList(
                    "iPhone 6S", "iPhone 6", "iPhone 5", "Samsung Galaxy 4",
                     "Lumia Nokia"));
            
            System.out.println("list of phones: " + listOfPhones);
            
            // Iterating and removing objects from list
            // This is wrong way, will throw ConcurrentModificationException
            for(String phone : listOfPhones){
                if(phone.startsWith("iPhone")){
                   // listOfPhones.remove(phone); // will throw exception
                }
            }
            
            // The Right way, iterating elements using Iterator's remove() method
            
            for(Iterator<String> itr = listOfPhones.iterator();
                                       itr.hasNext();){            
                String phone = itr.next();            
                if(phone.startsWith("iPhone")){
                    // listOfPhones.remove(phone);  // wrong again
                    itr.remove(); // right call
                }
            }
            
            System.out.println("list after removal: " + listOfPhones);
    
        }
    
    }
    

    Source

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2017-01-25
      相关资源
      最近更新 更多