Java和XML(JAXP) - 缓存和线程安全怎么样? [英] Java and XML (JAXP) - What about caching and thread-safety?

查看:218
本文介绍了Java和XML(JAXP) - 缓存和线程安全怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 在使用 Java API for XML <时,我想知道哪些对象可以重用(在相同或不同的文档中) / em>处理,JAXP:

  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:


  • DocumentBuilderFactory

  • DocumentBuilder

  • XPath

  • 节点

  • ErrorHandler (编辑:我忘了这必须在我自己的代码中实现,抱歉)

  • DocumentBuilderFactory
  • DocumentBuilder
  • XPath
  • Node
  • ErrorHandler ( 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:



DocumentBuilderFactory的实现class不是
保证是线程安全的。用户应用程序上涨
以确保
关于使用
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.




  • http://download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html

    • http://download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
    • 从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:

      • 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())

      XPath不是线程安全的,来自Javadoc

      XPath is not thread safe, from the Javadoc


      XPath对象不是线程-safe和
      不可重入。换句话说,应用程序对
      的责任是
      确保一个XPath对象不是在给定时间的任何
      的多个线程中使用的
      ,而且是$调用b $ b方法,应用程序可能
      不递归调用evaluate
      方法。

      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.




      • http:// download-llnw.oracle.com/javase/6/docs/api/javax/xml/xpath/XPath.html

        • http://download-llnw.oracle.com/javase/6/docs/api/javax/xml/xpath/XPath.html
        • 节点不是线程安全的,来自Xerces网站

          Node is not thread safe, from Xerces website


          Xerces DOM实现
          线程安全吗?不.DOM没有
          要求实现为线程
          安全。如果您需要从多个线程访问DOM
          ,则需要
          将相应的锁
          添加到您的应用程序代码中。

          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.




          • http://xerces.apache.org/xerces2-j/faq-dom.html#faq-1

            • http://xerces.apache.org/xerces2-j/faq-dom.html#faq-1
            • 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:

              • http://en.wikipedia.org/wiki/Thread_safety

              这篇关于Java和XML(JAXP) - 缓存和线程安全怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆