gpt4 book ai didi

多线程 Java 代码的 java.util.ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-01 18:07:42 28 4
gpt4 key购买 nike

我在“String name = (String)keys.next();”处收到 ConcurrentModificationException当我在多线程中运行java代码时,一行。导致错误的函数部分如下:

 if (inputStr == null)
return null;

String tempStr = new String(inputStr);

Properties props = System.getProperties();
Iterator keys = props.keySet().iterator();

while (keys.hasNext()) {
String name = (String) keys.next();
String value = props.getProperty(name);

//Build a pattern and compile it
String patternStr = "%" + name + "%";
Pattern pattern = Pattern.compile(patternStr);

// Replace all occurrences of pattern in input string
Matcher matcher = pattern.matcher(tempStr);
tempStr = matcher.replaceAll(value);
}

堆栈跟踪详细信息如下:

Caused by: java.util.ConcurrentModificationException at java.util.Hashtable$Enumerator.next(Unknown Source) at com.pb.common.util.ResourceUtil.replaceWithSystemPropertyValues(ResourceUtil.java:350) at com.pb.common.calculator.ControlFileReader.searchAndReplaceWithEnv(ControlFileReader.java:610) at com.pb.common.calculator.ControlFileReader.readMatrixDataEntries(ControlFileReader.java:493) at com.pb.common.calculator.ControlFileReader.(ControlFileReader.java:109) at misc.UtilityExpressionCalculator.(UtilityExpressionCalculator.java:191) at misc.AbstractChoiceHandler.createUtilityExpressionCalculator(AbstractChoiceHandler.java:84) at logSumCalculator.ModeChoiceModel.(ModeChoiceModel.java:41) at logSumCalculator.ModeChoiceTask.run(ModeChoiceTask.java:59)

迭代键的方式是否存在问题?

最佳答案

您需要在迭代时同步属性:

Properties props = System.getProperties();
synchronized(props) {
// do work here ...
}

关于多线程 Java 代码的 java.util.ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067010/

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