英文:
ConcurrentHashMap of Singleton Instances
问题
我看到了一个在Quora上的回答(https://www.quora.com/Can-we-create-mulitple-instances-of-singleton-classes-in-Java-if-so-how-if-not-then-why),建议创建一个ConcurrentHashMap,将Singleton实例“按标识字符串键入”,但我不确定这将如何工作。以下是我的尝试:
public class SingletonClass {
static ConcurrentHashMap<String, SingletonClass> list = new ConcurrentHashMap<String, SingletonClass>();
private static SingletonClass instance = null;
static String name;
public SingletonClass() {
this.name = "";
}
public SingletonClass(String name) {
this.name = name;
}
public static SingletonClass getInstance(String key) {
SingletonClass result = list.get(key);
if (result == null) {
instance = new SingletonClass(name);
list.putIfAbsent(key, instance);
result = instance;
}
return result;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
我不知道如何继续或如何创建此类的实例并在列表中存储键值对,然后根据特定的键检索值?任何帮助将不胜感激。
英文:
I saw an answer on quora (https://www.quora.com/Can-we-create-mulitple-instances-of-singleton-classes-in-Java-if-so-how-if-not-then-why) that suggested creating a ConcurrentHashMap of Singleton instances 'keyed by an identifying string', but I'm unsure of how this would work. Here is my attempt:
public class SingletonClass {
static ConcurrentHashMap<String, SingletonClass> list = new ConcurrentHashMap<String, SingletonClass>();
private static SingletonClass instance = null;
static String name;
public SingletonClass() {
this.name = "";
}
public SingletonClass(String name) {
this.name = name;
}
public SingletonClass getInstance(String key) {
SingletonClass result = list.get(key);
if(result == null) {
instance = new SingletonClass(name);
list.putIfAbsent(key, instance);
result = instance;
}
return result;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
And I have no idea how to proceed or how to create an instance of this and store a key,value pair in the list, and then retrieve the value again depending on the specific key? Any help would be much appreciated.
答案1
得分: 2
- 你创建了多个实例,这怎么能算是单例模式呢?这不是单例模式。
getInstance()
在任何情况下都应该是静态的。- 你在
getInstance()
方法中使用了一个静态变量,没有任何理由,这样会引发竞态条件(实际上是多个竞态条件)。而且这个变量是不必要的,应该将所有操作放在这个方法的范围内。 - 这不是单例模式,这是对象池中的享元模式,类似于Java中的包装类(Integer、Long等)。
重点是,使用这种方法,不会有两个具有相同键的对象。
// 使用你的方法
SingletonClass one = SingletonClass.getInstance("someKey");
SingletonClass two = SingletonClass.getInstance("someKey");
System.out.println(one == two); // true,同一个对象
// 不使用你的方法
SingletonClass three = new SingletonClass("someKey");
SingletonClass four = new SingletonClass("someKey");
System.out.println(three == four); // false,不同的对象
可以节省内存,可能节省对象创建的时间,也可能节省垃圾回收周期的时间。
英文:
- you are creating multiple instances, how is that a singleton then? It's not
getInstance()
should be static in any case- you are using a static variable in your
getInstance()
method for no reason, you open this code to a racing condition this way (well, multiple racing conditions). And this variable is unnecessary, do everything in scope of this method - this is not a singleton pattern, this is an object pool of flyweight objects - same as wrapper classes (Integer, Long etc.) in Java
The point is that, with this approach, there will be no 2 objects with the same key.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-java -->
// with your approach
SingletonClass one = SingletonClass.getInstance("someKey");
SingletonClass two = SingletonClass.getInstance("someKey");
System.out.println(one == two); // true, same object
// without your approach
SingletonClass three = new SingletonClass("someKey");
SingletonClass four = new SingletonClass("someKey");
System.out.println(three == four); // false, different objects
<!-- end snippet -->
Saving memory, possibly time on object creation and possibly time on gc cycles.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论