看起来 Java 系统属性的迭代版本不是线程安全的。可以使用下面的代码进行确认:
import java.io.FileWriter;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
public class JavaPropertiesTest {
public static void main(String[] args) throws Exception {
new Thread(() -> {
while (true) {
Properties properties = System.getProperties();
try (FileWriter fileOutputWriter = new FileWriter("/dev/null")){
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
fileOutputWriter.append(entry.getKey().toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
Random gen = new Random();
while (true) {
System.getProperties().setProperty(Integer.toString(gen.nextInt()), Integer.toString(gen.nextInt()));
}
}).start();
}
}
在 clojure 1.8.x 中的堆栈跟踪,最新版本中也同样存在问题
导致:java.util.ConcurrentModificationException at java.util.Hashtable$Enumerator.next(Hashtable.java:1387) at clojure.lang.RT$4.invoke(RT.java:509) at clojure.lang.LazySeq.sval(LazySeq.java:40) at clojure.lang.LazySeq.seq(LazySeq.java:49) at clojure.lang.ChunkedCons.chunkedNext(ChunkedCons.java:59) at clojure.lang.ChunkedCons.next(ChunkedCons.java:43) at clojure.lang.RT.next(RT.java:688) at clojure.core$next4341.invokeStatic(core.clj:64) at clojure.core.protocols$naive_seq_reduce.invokeStatic(protocols.clj:63) at clojure.core.protocols$interface_or_naive_reduce.invokeStatic(protocols.clj:72) at clojure.core.protocols$fn6755.invokeStatic(protocols.clj:168) at clojure.core.protocols$fn6755.invoke(protocols.clj:124) at clojure.core.protocols$fn6710$G6705__6719.invoke(protocols.clj:19) at clojure.core.protocols$seq_reduce.invokeStatic(protocols.clj:31) at clojure.core.protocols$fn6732.invokeStatic(protocols.clj:75) at clojure.core.protocols$fn6732.invoke(protocols.clj:75) at clojure.core.protocols$fn6684$G66796697.invoke(protocols.clj:13) at clojure.core$reduce.invokeStatic(core.clj:6545) at clojure.core.server$parse_props.invokeStatic(server.clj:143) at clojure.core.server$start_servers.invokeStatic(server.clj:154) at clojure.core.server$start_servers.invoke(server.clj:154) at clojure.lang.Var.invoke(Var.java:379) at clojure.lang.RT.doInit(RT.java:482) at clojure.lang.RT.