在多线程环境中使用 Java Singleton 实例 [英] Using Java Singleton instance in a multi threaded environment

查看:80
本文介绍了在多线程环境中使用 Java Singleton 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application wants to communicate with a REST server. At first, we need to authenticate and in response to that, we will receive a client token. This token is valid for 30 minutes and for the rest of the communication this client token needs to be present as a header.

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

I am planning to implement a Singleton Class that handles the REST communication. Reson for not following an ENUM approach (as mentioned in many other threads) is because of the requirement to make a rest call and populate the client token in runtime.

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
    } 

}

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

The main challenge here is that this class will be used by multiple threads at the same time (client token is the same for all threads). My doubt is about the initialization part where we make a REST call to populate client token. Is there any possible chances of ambiguity here between threads if the REST call takes a couple of seconds to set the client token. If yes, what do you think is the best way to implement this?

推荐答案

如果你有一个多线程应用并且使用单例模式,你可以使用synchronized关键字.

If you have a multithread application and use singleton pattern, you can use synchronized keyword.

public static synchronized RESRService getInstance(){}

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

But getInstance() method is synchronized so it causes slow performance as multiple threads can’t access it simultaneously and all operation is synchronized. So you can use Double checked locking solution.

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;
}

这篇关于在多线程环境中使用 Java Singleton 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆