gpt4 book ai didi

java - 单例类中双重检查锁定模式的潜在问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:24 27 4
gpt4 key购买 nike

我相信我编写的以下 Singleton 类是 Thread Safe

双重检查锁定模式显然在某些情况下可能会遇到问题(我看到人们警告不要这样做,虽然那是不久前的事所以我现在只是在谷歌上搜索答案)

我现在不确定我下面的 Singleton 类中的 Double Checked 锁定模式是否会出现任何问题。我添加了双重检查锁定模式,使程序运行得更快。

    public class CassandraAstyanaxConnection {

private static CassandraAstyanaxConnection _instance;
private static final Object syncObject = new Object();
private AstyanaxContext<Keyspace> context;
private Keyspace keyspace;
private ColumnFamily<String, String> emp_cf;


public static CassandraAstyanaxConnection getInstance() {
if (_instance == null) {
synchronized(syncObject) {
if (_instance == null) {
_instance = new CassandraAstyanaxConnection();
}
}
}
return _instance;
}

/**
* Creating Cassandra connection using Astyanax client
*
*/
private CassandraAstyanaxConnection() {

context = new AstyanaxContext.Builder()
.forCluster(ModelConstants.CLUSTER)
.forKeyspace(ModelConstants.KEYSPACE)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(9160)
.setMaxConnsPerHost(1)
.setSeeds("127.0.0.1:9160")
)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setCqlVersion("3.0.0")
.setTargetCassandraVersion("1.2"))
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());

context.start();
keyspace = context.getEntity();

emp_cf = ColumnFamily.newColumnFamily(
ModelConstants.COLUMN_FAMILY,
StringSerializer.get(),
StringSerializer.get());
}

/**
* returns the keyspace
*
* @return
*/
public Keyspace getKeyspace() {
return keyspace;
}

public ColumnFamily<String, String> getEmp_cf() {
return emp_cf;
}
}

我的代码在上面的 Singleton 类中是否存在双重检查锁定模式的问题?

创建线程安全的单例类的最佳方法是什么。 Holder Class Idiom 又如何呢?我可以在我的 Singleton 类中使用它吗?

基于我上面的单例类的任何示例都将帮助我更好地理解如何编写更好的线程安全单例类。

感谢您的帮助。

更新代码:-

经过一些建议,我修改了代码-

public class CassandraAstyanaxConnection {

private static class ConnectionHolder {
public static CassandraAstyanaxConnection connection = new CassandraAstyanaxConnection()
}

public static CassandraAstyanaxConnection getInstance() {
return ConnectionHolder.connection;
}

/**
* Creating Cassandra connection using Astyanax client
*
*/
private CassandraAstyanaxConnection() {

context = new AstyanaxContext.Builder()
.forCluster(ModelConstants.CLUSTER)
.forKeyspace(ModelConstants.KEYSPACE)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(9160)
.setMaxConnsPerHost(1)
.setSeeds("127.0.0.1:9160")
)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setCqlVersion("3.0.0")
.setTargetCassandraVersion("1.2"))
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());

context.start();
keyspace = context.getEntity();

emp_cf = ColumnFamily.newColumnFamily(
ModelConstants.COLUMN_FAMILY,
StringSerializer.get(),
StringSerializer.get());
}

/**
* returns the keyspace
*
* @return
*/
public Keyspace getKeyspace() {
return keyspace;
}

public ColumnFamily<String, String> getEmp_cf() {
return emp_cf;
}
}

如果这看起来正确,请告诉我。

最佳答案

我非常喜欢以下延迟初始化单例模式:

public final class CassandraAstyanaxConnection {

...

private static class ConnectionHolder {
public static CassandraAstyanaxConnection connection = new CassandraAstyanaxConnection()
}

public static CassandraAstyanaxConnection getInstance() {
return ConnectionHolder.connection;
}

...
}

关于java - 单例类中双重检查锁定模式的潜在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099266/

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