Java和XML (JAXP)——那么缓存和线程安全呢?

时间:2022-02-17 21:00:21
  1. 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:

    我想知道在使用Java API进行XML处理时,哪些对象可以重用(在相同或不同的文档中),JAXP:

    • DocumentBuilderFactory
    • DocumentBuilderFactory
    • DocumentBuilder
    • DocumentBuilder
    • XPath
    • XPath
    • Node
    • 节点
    • ErrorHandler (EDIT: I forgot that this has to be implemented in my own code, sorry)
    • ErrorHandler(编辑:我忘记了这必须在我自己的代码中实现,抱歉)
  2. Is it recommended to cache those objects or do the JAXP implementations already cache them?

    建议缓存这些对象,还是JAXP实现已经缓存了它们?

  3. Is the (re)use of those objects thread-safe?

    这些对象的(重新)使用是线程安全的吗?

1 个解决方案

#1


27  

Reuse

重用

In the same thread those objects can and should be reused. For example you can use the DocumentBuilder to parse multiple documents.

在同一个线程中,这些对象可以并且应该被重用。例如,您可以使用DocumentBuilder来解析多个文档。

Thread Safety

线程安全

DocumentBuilderFactory used to explicity state it was not thread safe, I believe this is still true:

DocumentBuilderFactory用来说明它不是线程安全的,我相信这仍然是正确的:

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.

DocumentBuilderFactory类的实现不能保证线程安全。用户应用程序可以从多个线程中确保使用DocumentBuilderFactory。

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:

从堆栈溢出中,DocumentBuilder似乎也不是线程安全的。但是在Java SE 5中增加了重置方法,使您可以重用文档构建器:

XPath is not thread safe, from the Javadoc

从Javadoc来看,XPath不是线程安全的

An XPath object is not thread-safe and not reentrant. In other words, it is the application's responsibility to make sure that one XPath object is not used from more than one thread at any given time, and while the evaluate method is invoked, applications may not recursively call the evaluate method.

XPath对象不是线程安全的,也不是可重入的。换句话说,应用程序的职责是确保在任何给定的时间内不会从多个线程中使用一个XPath对象,并且在调用evaluate方法时,应用程序可能不会递归地调用evaluate方法。

Node is not thread safe, from Xerces website

节点不是线程安全的,从Xerces网站

Is Xerces DOM implementation thread-safe? No. DOM does not require implementations to be thread safe. If you need to access the DOM from multiple threads, you are required to add the appropriate locks to your application code.

Xerces DOM实现线程安全吗?不。DOM不要求实现是线程安全的。如果需要从多个线程访问DOM,则需要向应用程序代码中添加适当的锁。

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:

ErrorHandler是一个接口,所以要由接口的实现来确保线程安全。关于线程安全的指针,您可以从这里开始:

#1


27  

Reuse

重用

In the same thread those objects can and should be reused. For example you can use the DocumentBuilder to parse multiple documents.

在同一个线程中,这些对象可以并且应该被重用。例如,您可以使用DocumentBuilder来解析多个文档。

Thread Safety

线程安全

DocumentBuilderFactory used to explicity state it was not thread safe, I believe this is still true:

DocumentBuilderFactory用来说明它不是线程安全的,我相信这仍然是正确的:

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.

DocumentBuilderFactory类的实现不能保证线程安全。用户应用程序可以从多个线程中确保使用DocumentBuilderFactory。

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:

从堆栈溢出中,DocumentBuilder似乎也不是线程安全的。但是在Java SE 5中增加了重置方法,使您可以重用文档构建器:

XPath is not thread safe, from the Javadoc

从Javadoc来看,XPath不是线程安全的

An XPath object is not thread-safe and not reentrant. In other words, it is the application's responsibility to make sure that one XPath object is not used from more than one thread at any given time, and while the evaluate method is invoked, applications may not recursively call the evaluate method.

XPath对象不是线程安全的,也不是可重入的。换句话说,应用程序的职责是确保在任何给定的时间内不会从多个线程中使用一个XPath对象,并且在调用evaluate方法时,应用程序可能不会递归地调用evaluate方法。

Node is not thread safe, from Xerces website

节点不是线程安全的,从Xerces网站

Is Xerces DOM implementation thread-safe? No. DOM does not require implementations to be thread safe. If you need to access the DOM from multiple threads, you are required to add the appropriate locks to your application code.

Xerces DOM实现线程安全吗?不。DOM不要求实现是线程安全的。如果需要从多个线程访问DOM,则需要向应用程序代码中添加适当的锁。

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:

ErrorHandler是一个接口,所以要由接口的实现来确保线程安全。关于线程安全的指针,您可以从这里开始: