In Jetty source code, the jetty-xml module, XmlConfiguration has the following code:
java private static final XmlParser __parser = initParser();
private synchronized static XmlParser initParser() {
XmlParser parser = new XmlParser();
URL config60 = Loader.getResource(XmlConfiguration.class, "org/eclipse/jetty/xml/configure_6_0.dtd");
URL config76 = Loader.getResource(XmlConfiguration.class, "org/eclipse/jetty/xml/configure_7_6.dtd");
URL config90 = Loader.getResource(XmlConfiguration.class, "org/eclipse/jetty/xml/configure_9_0.dtd");
parser.redirectEntity("configure.dtd", config90);
parser.redirectEntity("configure_1_0.dtd", config60);
parser.redirectEntity("configure_1_1.dtd", config60);
...
return parser;
the __parser variable use the static method initParser() initialization.the __parser should be thread-safe,only load once by the classloader, why the initParser() need to use synchronized? Whether excess?
Further explanations:I debug the Jetty source code, from the jetty-start module, then invoke the jetty-xml module.
XmlConfiguration.class?synchronizemight give an insight.jetty-startmodule, then invoke thejetty-xmlmodule, eg, invoke theXmlConfiguration#main()method.InXmlConfigurationclass, in theXmlConfigurationconstructor, synchronized on__parser,eg.synchronized (__parser) { ... }, the source code can be fount on github, github.com/eclipse/jetty.project/blob/master/jetty-xml/src/main/….synchronized(__parser)is a different monitor. Thegit blamealso doesn't give an insight. For me it's a mystery. But I'm curious as well.