gpt4 book ai didi

Java 类的 getter/setter 方法和单例模式中的细粒度同步

转载 作者:行者123 更新时间:2023-12-02 00:14:55 27 4
gpt4 key购买 nike

我正在尝试使用 synchronization java 指令在类中实现细粒度同步,即同步尽可能少的代码。我将内联注释代码,以解释一下我做了什么,在代码之后我会问你如何改进代码:

public class MyClass {
private static volatile MyClass singletonInstance = null;

private HashMap<String, Integer> mHashMap = null;
private String mStringA = null;
private String mStringB = null;


// Use double check technique to use synchronization only
// at the first getInstance() invocation
public static MyClass getInstance() {
if (singletonInstance == null) {
synchronized (MyClass.class) {
if (singletonInstance == null)
singletonInstance = new MyClass();
// Initialize class member variables
singletonInstance.mHashMap = new HashMap<String,Integer>();
singletonInstance.mStringA = new String();
singletonInstance.mStringB = new String();
}

}
return singletonInstance;
}

// The following two methods manipulate the HashMap mHashMap
// in a secure way since they lock the mHashMap instance which
// is always the same and is unique
public Integer getIntegerFromHashmap(String key) {
synchronized (mHashMap) {
return mHashMap.get(key);
}
}

public void setIntegerIntoHashmap(String key, Integer value) {
synchronized (mHashMap) {
mHashMap.put(key, value);
}
}

// With the two String members mStringA and mStringB the problem is
// that the instance of String pointed by the member is varied by the
// setter methods, so we can not lock in a fine grained way and we
// must lock on the singletonInstance.
public String getStringA() {
synchronized (singletonInstance) {
return mStringA;
}
}

public String getStringB() {
synchronized (singletonInstance) {
return mStringB;
}
}

public void setStringA(String newString) {
synchronized (singletonInstance) {
mStringA = newString;
}
}

public void setStringB(String newString) {
synchronized (singletonInstance) {
mStringB = newString;
}
}
}

我不喜欢两个 String 成员变量的 getter 和 setter 方法,因为锁定 singletonInstance 会使线程尝试访问 mStringB 等待,直到正在操作 mStringA 的线程释放其锁。在这种情况下你会怎么做?您是否会在 MyClass 中创建两个成员变量,如 private final Integer mStringALock = new Integer(0)private final Integer mStringBLock = new Integer(0) > 并分别在 mStringAmStringB 的 getter 和 setter 方法的同步块(synchronized block)中使用它们?

如果您对如何改进上述代码以及 String 成员变量的细粒度同步的建议变体有一些想法,欢迎您:)

最佳答案

通常更简单的解决方案更容易实现。我还会使用 2004 年添加的并发库。

这不需要显式锁,并且每个容器都是线程安全的。

您可以使用 AtomicReference,但在这种情况下,它不会为您提供 volatile 尚未提供的任何内容。 (正如 kdgregory 指出的)您可以在更复杂的情况下使用 AtomicReference。

public enum MyClass {
INSTANCE;

private final Map<String, Integer> mHashMap = new ConcurrentHashMap<String, Integer>();
private volatile String mStringA = null;
private volatile String mStringB = null;


// The following two methods manipulate the HashMap mHashMap
// in a secure way
public Integer getIntegerFromHashmap(String key) {
return mHashMap.get(key);
}

public void setIntegerIntoHashmap(String key, Integer value) {
mHashMap.put(key, value);
}

public String getStringA() {
return mStringA;
}

public String getStringB() {
return mStringB;
}

public void setStringA(String newString) {
mStringA = newString;
}

public void setStringB(String newString) {
mStringB = newString;
}
}

关于Java 类的 getter/setter 方法和单例模式中的细粒度同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com