在程序执行期间获得“罕见”的ConcurrentModificationException

时间:2018-01-08 14:28:13

标签: java xstream optaplanner concurrentmodification

我正在使用Optaplanner创建一个程序,我不断得到这个例外,但只是在罕见的场合,大多数时候我可以毫无问题地执行我的程序,并且很难重新创建问题,因为它有时只会发生..

Exception in thread "main" java.util.ConcurrentModificationException: java.util.ConcurrentModificationException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	...
 	at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:503)
	at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:331)
	at org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:220)
	at org.optaplanner.core.impl.solver.AbstractSolverFactory.buildSolver(AbstractSolverFactory.java:61)
	at org.avalin.optaplanner.main.EmployeeRoster.buildEvaluationSolver(EmployeeRoster.java:247)
	at org.avalin.optaplanner.main.EmployeeRoster.main(EmployeeRoster.java:82)
Caused by: java.util.ConcurrentModificationException
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1138)
  ...
  	at java.base/java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:291)
	at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:747)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:283)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1603)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)

这是似乎发生的buildEvaluationSolver方法:

private static Solver buildEvaluationSolver()
{
    SolverFactory solverFactory = SolverFactory.createFromXmlResource(EVALUATION_CONFIG_XML);
    return solverFactory.buildSolver();
}

我假设某些东西在同时被更改时被迭代,但对我来说最困难的部分是弄清楚如何/在哪里确切,如果它是我的代码,在创建fromxmlresource或第三次时使用xstream。

希望这对某些人有意义

编辑:我了解到我应该包括警告

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/Users/path/my.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

问题似乎源于得到此错误的计算机使用的是JDK9,它通常可以与JDK8一起使用。我们在给定的计算机上降级为JDK8,并且从那以后没有看到错误。

1 个答案:

答案 0 :(得分:0)

您可能通过调用echo $GOOGLE_APPLICATION_CREDENTIALS 从不同线程修改相同的solverFactory实例(例如动态配置终止时间限制)。

解决方案:查看solverFactory.getSolverConfig()的文档。