Is DocumentBuilderFactory thread-safe in Java 5+?

☆樱花仙子☆ 提交于 2019-12-06 17:04:23

问题


The Java 1.4 doc for javax.xml.parsers.DocumentBuilderFactory states in no uncertain terms:

An implementation of the DocumentBuilderFactory class is NOT guaranteed to be thread safe. It is up to the user application to make sure about the use of the DocumentBuilderFactory from more than one thread. Alternatively the application can have one instance of the DocumentBuilderFactory per thread. An application can use the same instance of the factory to obtain one or more instances of the DocumentBuilder provided the instance of the factory isn't being used in more than one thread at a time.

The Java 5 and Java 6 docs, however, say nothing of the kind. Is DocumentBuilderFactory thread-safe now, or did Sun just decide the warning was overkill and people should know better?


回答1:


According to the documentation of the singleton DocumentBuilderFactory the newInstance method is not sychronized and then, the DocumentBuilderFactory is still not a thread safe in Java 8... Neither in Java 11. Actually, the constructor didn't change since Java 1.4.




回答2:


Since it would've been in extremely poor taste to make it thread safe without telling my money is that it's still unsafe. You can test this yourself using breakpoints.



来源:https://stackoverflow.com/questions/9828254/is-documentbuilderfactory-thread-safe-in-java-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!