synchronized(compiledStylesheets) {
if(compiledStylesheets.containsKey(xslt)) {
exec = compiledStylesheets.get(xslt);
} else {
exec = compile(s, imports);
compiledStylesheets.put(xslt, exec);
}
}
compiledStylesheets
是一个私有、终态的 HashMap
。我有几个问题。
compile()
方法返回需要几百毫秒,这看起来很长,但我没有看到其他的替代方案。此外,在 synchronized
块中使用 Collections.synchronizedMap 是不必要的,对吗?除了初始化/实例化之外,这是唯一使用该对象的代码。
另外,我知道存在一种 ConcurrentHashMap
,但我不知道是否有点大材小用。在这种情况下,putIfAbsent()
方法将无法使用,因为它不能跳过 compile()
方法调用。我也不知道它是否会解决“在 containsKey()
之后但在 put()
之前被修改”的问题,或者在这种情况下是否真的是一个问题。
编辑:拼写错误
ConcurrentHashMap
。 - Boris the Spiderreturn exec
。 - assylias