简体   繁体   English

遍历(读取)静态DOM文档对象是否是线程安全的?

[英]Traverse (Read) static DOM document object is thread-safe or not?

I created a DOM document static object, such as below, it uses javax.xml.parsers.* and org.w3c.dom.* API: 我创建了一个DOM文档静态对象,如下所示,它使用javax.xml.parsers。*和org.w3c.dom。* API:

        DocumentBuilderFactory docBldrFactry = DocumentBuilderFactory.newInstance();
        docBldrObj = docBldrFactry.newDocumentBuilder();

        File file = new File(fileDirectory);
        // Parse the XML file and return a DOM document object

        document = docBldrObj.parse(file);
        //FYI, document is declared as private static org.w3c.dom.Document document elsewhere. 

Later after created above, If this static DOM document object shared by threads, but all threads are just read (traverse) this document, is it thread safe? 稍后在上面创建之后,如果此静态DOM文档对象由线程共享,但是仅读取(遍历)此文档的所有线程,那么线程安全吗?

I assume it is since read should not modify this shared state, but not sure whether internally there is some magic about it which I don't know. 我认为是因为读取不应修改此共享状态,但是不确定内部是否存在我不知道的魔术。

Thanks 谢谢

The problem solved by writing own simple Document structure. 通过编写自己的简单Document结构解决了该问题。 Eg, clone the DOM document into that, which is thread-safe on read operations. 例如,将DOM文档克隆到其中,这对读取操作是线程安全的。 FYI, for my own purpose, when cloning the document, I don't clone everything but the information based on my need (COMMENT_NODE, TEXT_NODE, ELEMENT_NODE, attributes). 仅供参考,出于我自己的目的,克隆文档时,除了基于我的需要的信息(COMMENT_NODE,TEXT_NODE,ELEMENT_NODE,属性)以外,我不会克隆所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM