2

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.

6
  • Is there anything else synchronizing on XmlConfiguration.class? Commented Oct 22, 2014 at 6:50
  • Atomicity quite possibly? Commented Oct 22, 2014 at 6:52
  • Maybe a look into the commit which introduces that synchronize might give an insight. Commented Oct 22, 2014 at 6:55
  • Sorry, I can't reply to everyone, maybe I have not 50 reputation.Here, I add some content.I debug the Jetty Source Code, from the jetty-start module, then invoke the jetty-xml module, eg, invoke the XmlConfiguration#main() method.In XmlConfiguration class, in the XmlConfiguration constructor, 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/…. Commented Oct 22, 2014 at 6:58
  • synchronized(__parser) is a different monitor. The git blame also doesn't give an insight. For me it's a mystery. But I'm curious as well. Commented Oct 22, 2014 at 7:02

2 Answers 2

2

I don't think it needs to be synchronized at all.

The Java Language Specification guarantees that Java class initialization (i.e. the initialization of the classes statics, etcetera) is performed within a lock to prevent race conditions. This applies whether the class is loaded once, or many times (i.e. by different class loaders).

I suspect that the author of this code was simply not aware of how the JVM handles this, and is taking unnecessary precautions.

(On the other hand, these "belt and braces" precautions are harmless, and the performance impact is trivial: probably unmeasurable.)


For the record, the procedure for class initialization is specified in the JLS in Section 12.4.2.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes,I agree.I had submit a bug to jetty, asked why.Take a look at how they reply.
@ykgarfield - I predict they will close it with status "won't fix" or similar. If it ain't broke, don't fix it!
ha ha,I receive the autor reply, the synchronized is not necessary,They will fixed it.Anyhow, Thanks again.:)
0

If you make sure that there is at most one class loader then there is no need for synchronization. The static initialization code is run once per class loader.

From what i can see in the Jetty WebAppContext only one class loader can be set in configuration.

1 Comment

Anyhow, Thanks, the author will fix it.The synchronized is not necessary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.