问题描述
-
在使用 Java API for XML 对象可以重用(在相同或不同的文档中) / em>处理,JAXP:
I'd like to know which objects can be reused (in the same or different document) when using the Java API for XML processing, JAXP:
-
DocumentBuilderFactory
-
DocumentBuilder
-
XPath
-
节点
- (编辑:我忘了这必须在我自己的代码中实现,抱歉)
DocumentBuilderFactory
DocumentBuilder
XPath
Node
- ( I forgot that this has to be implemented in my own code, sorry)
建议缓存那些对象或者JAXP实现是否已经缓存它们?
Is it recommended to cache those objects or do the JAXP implementations already cache them?
这些是(重新)使用对象线程安全?
推荐答案
重用
在同一个线程中,这些对象可以而且应该被重用。例如,您可以使用DocumentBuilder来解析多个文档。
In the same thread those objects can and should be reused. For example you can use the DocumentBuilder to parse multiple documents.
线程安全
DocumentBuilderFactory用于说明它不是线程安全的,我相信这仍然是正确的:
DocumentBuilderFactory used to explicity state it was not thread safe, I believe this is still true:
- http://download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
- (关于DocumentBuilder.reset())
- Is DocumentBuilder.parse() thread safe?
- http://download-llnw.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#reset()
- http://www.junlu.com/msg/289939.html (about DocumentBuilder.reset())
- http://download-llnw.oracle.com/javase/6/docs/api/javax/xml/xpath/XPath.html
- http://xerces.apache.org/xerces2-j/faq-dom.html#faq-1
- http://en.wikipedia.org/wiki/Thread_safety
从Stack Overflow,DocumentBuilder似乎也不是线程安全的。但是在Java SE 5中添加了一个重置方法,允许您重用DocumentBuilders:
From Stack Overflow, DocumentBuilder does not appear to be thread safe either. However in Java SE 5 a reset method was added to allow you to reuse DocumentBuilders:
XPath不是线程安全的,来自Javadoc
XPath is not thread safe, from the Javadoc
节点不是线程安全的,来自Xerces网站
Node is not thread safe, from Xerces website
ErrorHandler是一个接口,因此由您的接口实现以确保线程安全。关于线程安全的指针你可以从这里开始:
ErrorHandler is an interface, so it is up to your implementation of that interface to ensure thread-safety. For pointers on thread-safety you could start here:
这篇关于Java和XML(JAXP) - 缓存和线程安全怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!