我使用标准的javax.xml包来解析linux机器上的一些XML文件。我的代码如下:
try
{
// Prepare parser
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.parse(file.getAbsolutePath()); // This is line 397
XPath xPath = xPathFactory.newXPath();
...
}
catch(IOException e) { ... }
单个DocumentBuilderFactory由多个线程访问,单个XPathFactory也是如此,我相信这是可接受的用法。使用上面的代码解析XML文件时,我偶尔会看到以下错误。
java.io.IOException: Bad file descriptor
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:229)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
at java.io.BufferedInputStream.read(BufferedInputStream.java:246)
at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at mypackage.MyXmlParser.parseFile(MyXmlParser.java:397)
at mypackage.MyXmlParser.access$500(MyXmlParser.java:51)
at mypackage.MyXmlParser$1.call(MyXmlParser.java:337)
at mypackage.MyXmlParser$1.call(MyXmlParser.java:328)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:284)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:665)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:690)
at java.lang.Thread.run(Thread.java:799)
我偶尔(约10%的时间)会看到以下附加文字:
Caused by:
java.io.IOException: Bad file descriptor
at org.apache.xml.serializer.ToStream.flushWriter(ToStream.java:260)
at org.apache.xml.serializer.ToXMLStream.endDocument(ToXMLStream.java:191)
at org.apache.xalan.transformer.TransformerIdentityImpl.endDocument(TransformerIdentityImpl.java:983)
at org.apache.xml.serializer.TreeWalker.traverse(TreeWalker.java:174)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:410)
... 9 more
当我手动检查文件时,我发现失败的文件和传递的文件之间没有区别。我可以确认传递的文件是有效的XML并且没有特殊字符或过早结束。
有谁知道为什么会发生这种情况,以及如何避免它?
> java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build pxa64dev-20061002a (SR3) )
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 Linux amd64-64 j9vmxa6423-20061001 (JIT enabled)
J9VM - 20060915_08260_LHdSMr
JIT - 20060908_1811_r8
GC - 20060906_AA)
JCL - 20061002
答案 0 :(得分:2)
它看起来像是并发线程的问题。
错误可能在您向我们展示的codelet之外的某处。但是对于DocumentBuilderFactory和XPathFactory,我不确定它们是否是线程安全的;它没有在文档中提到。
对于第一个测试,我建议您将解析XML文件的整个代码放入synchronized {}
子句中。如果这可以解决您的问题,那么它肯定是一个多线程问题。在这种情况下,您必须找出必须同步的代码的最小部分。