下面的类显示了与真实用例类似的内容。它始终为同一线程返回相同的实例。
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();
}
}
你会怎么调用它?是“工厂”吗? “值(value)持有者”? “ThreadLocalStore”?
最佳答案
有些人简单地将其称为 ThreadLocal Pattern 。另一个已知名称是 Thread-local Storage (TLS) .
关于java - 设计模式名称: Is it a factory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3457753/