[英]Traverse (Read) static DOM document object is thread-safe or not?
我創建了一個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.
稍后在上面創建之后,如果此靜態DOM文檔對象由線程共享,但是僅讀取(遍歷)此文檔的所有線程,那么線程安全嗎?
我認為是因為讀取不應修改此共享狀態,但是不確定內部是否存在我不知道的魔術。
謝謝
通過編寫自己的簡單Document結構解決了該問題。 例如,將DOM文檔克隆到其中,這對讀取操作是線程安全的。 僅供參考,出於我自己的目的,克隆文檔時,除了基於我的需要的信息(COMMENT_NODE,TEXT_NODE,ELEMENT_NODE,屬性)以外,我不會克隆所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.