Java – what is the best mode or method to load a static cache?

Suppose I have the following (assuming only Java 1.4, so there is no generics):

public class CacheManager {
    static HashMap states;
    static boolean statesLoaded;

    public static String getState(String abbrev) {
        if(!statesLoaded) {
            loadStates();
        }
        return (String) states.get(abbrev);
    }

    private static void loadStates() {
        //JDBC stuff to load the data
        statesLoaded = true;
    }
}

In a highly loaded multithreaded environment such as web application server, if > this may be a problem in theory 1 thread tried to get and load the cache at the same time (further assume that there is no startup code on the web application to initialize the cache)

Just use collections Is synchronized map enough to solve this problem? If many threads are accessing it, is there a performance problem with the synchronized map returned when executing get()?

Or is it better to have an asynchronous HashMap and synchronize on the load method or boolean variable? I think if you sync any of them, you may eventually lock the class

For example, if the load method is synchronized, if two threads enter the getstates () method at the same time, and both see that stateloaded is false The first one is to get the lock of the method, load the cache and set statesloaded to true Unfortunately, the second thread has evaluated statesloaded as false and enters the load method once idle is locked Won't it continue and load the cache again?

Solution

In this case, the best way to load the cache is to use JVM static initialization:

public class CacheManager {
    private static final HashMap states = new HashMap();

    public static String getState(String abbrev) {
        return (String) states.get(abbrev);
    }

    static {
        //JDBC stuff to load the data
    }
}

The cache will be loaded the first time the class is used, and since static initialization is thread safe, the mapping will be safely populated Any subsequent call to retrieve a value can be done without involving any locking

It is always a good idea to use static initialization whenever possible It's safe, efficient, and usually very simple

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>