java – ThreadLocal资源
当我强烈引用持有资源的ThreadLocal时,如下例所示;
private final static ThreadLocal<InputStream> resource = new ThreadLocal<InputStream>()
{
@Override
protected InputStream initialValue()
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream input = loader.getResourceAsStream("doc/doc.xml");
if (input == null) throw new RuntimeException("Could not locate doc.xml");
return input;
}
};
在ThreadLocal中保存的InputStream在什么时间点/范围内是不可用的.我想知道在某些时候,如果没有使用ThreadLocal对象,JVM将垃圾收集它的引用,因此对嵌套的InputStream的访问将无法使用.
javadoc表明;
All of its copies of thread-local
instances are subject to garbage
collection (unless other references to
these copies exist).
如果是这种情况,那么如何确保副本始终可用?