下面的类显示了类似于真实用例的内容。它总是为同一个线程返回同一个实例。
public class LookingForName {
private static final ThreadLocal<Something> threadLocal =
new ThreadLocal<Something>(){
@Override
protected Something initialValue() {
return getSomethingSpecial(); // not relevant
}
};
/**
* @return always the same instance of "Something" for the current thread.
*/
public static Something getInstance() {
return threadLocal.get();
}
}
你会怎么称呼它?是“工厂”吗?“价值持有者”?“线程本地存储”?