在多线程环境中使用 Java 单例实例

Using Java Singleton instance in a multi threaded environment

我的应用程序想要与 REST 服务器通信。首先,我们需要进行身份验证,作为响应,我们将收到一个 客户端令牌 。此令牌的有效期为 30 分钟,对于其余的通信,此客户端令牌需要以 header.

的形式出现

我计划实现一个处理 REST 通信的单例 Class。不遵循 ENUM 方法的原因(如许多其他线程中所述)是因为需要进行休息调用并在运行时填充客户端令牌。

public class RESRService {

    private static RESRService RESTSERVICEINSTANCE;
    private String clientToken;

    private RESRService(){
        clientToken = //make a rest call to authenticate and get the client 
                      //token from reponse
    }

    public static RESRService getInstance(){
        if(RESTSERVICEINSTANCE == null){
            RESTSERVICEINSTANCE = new RESRService();
        }

        return RESTSERVICEINSTANCE;
    }

    public void makeRestCall(String requestSpecificInfo){
        //set client token to header
        //create JSON body using requestSpecificInfo
        //make rest call
    } 

}

这里的主要挑战是这个 class 将同时被多个线程使用(所有线程的客户端令牌都相同)。我怀疑我们进行 REST 调用以填充客户端令牌的初始化部分。如果 REST 调用需要几秒钟来设置客户端令牌,线程之间是否有可能出现歧义。如果是,您认为最好的实现方式是什么?

如果您有一个多线程应用程序并使用单例模式,则可以使用 synchronized 关键字。

public static synchronized RESRService getInstance(){}

但是 getInstance() 方法是同步的,所以它会导致性能下降,因为多个线程不能同时访问它并且所有操作都是同步的。所以你可以使用Double checked locking解决方案。

private static volatile RESTSERVICE RESTSERVICEINSTANCE;

public static RESTSERVICE getInstance(){
    RESTSERVICE restservice = RESTSERVICEINSTANCE;
    if(restservice == null){
        synchronized (RESTSERVICE.class){
            restservice = RESTSERVICEINSTANCE;
            if(restservice == null){
                restservice = new RESTSERVICE();
            }
        }
    }
    return restservice;
}