java - 多线程 Java 应用程序中的 xpathFactory 问题
问题描述
我有 xpathutility 类,我在整个应用程序中使用它来从 xpath 表达式获取字符串或节点结果:
public static String getStringResult(String expression, Document doc) {
String outString = new String();
try {
outString = (String)xpath.compile(expression).evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
e.printStackTrace(System.out);
}
return outString;
}
public static NodeList getNodeListResult(String expression, Document doc) {
NodeList outList = null;
try {
outList = (NodeList)xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
e.printStackTrace(System.out);
}
return outList;
}
对于工厂的初始化,我使用了以下内容,以使线程安全,通过互联网找到它:
private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>()
{
@Override
protected XPathFactory initialValue()
{
return XPathFactory.newInstance();
}
};
private static final XPath xpath= XPATH_FACTORY.get().newXPath();
但是当我从多个线程执行它时,它给了我对 xpath 的空指针期望。需要知道如何使其线程安全。
解决方案
推荐阅读
- typescript - TypeScript 可以推断对象的属性吗?
- vue.js - 为什么我的 Vue/Nuxt Select 字段状态默认有效?
- android - 这样的布局将如何实现?
- fortran - 在 Fortran 中创建的 HDF5 文件直到模拟结束才写入
- javascript - 悬停时更改 SVG 圆形平铺世界地图上区域的颜色
- javascript - 简化旧的 RegExp
- ios - 创建自定义 Xcode 项目模板
- c++ - c++向量如何
> 管理内存 - regex - Teradata 正则表达式
- google-chrome - Chrome 在重定向时忽略标头中的 CSP 指令