简体   繁体   中英

Is my Java cache thread-safe and performant?

I've implemented a little helper class that provides an easy fail-safe implementation of the enum's valueOf method. This means in case the value is not found, it returns null instead of an exception.

Here's the code:

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import java.io.Serializable;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * This permits to easily implement a failsafe implementation of the enums's valueOf method
 * </p>
 *
 * <p>
 * Basic usage exemple on an enum class called MyEnum:
 * FailSafeValueOf.get(MyEnum.class).valueOf("EnumName");
 * </p>
 *
 * @author Sebastien Lorber <i>(lorber.sebastien@gmail.com)</i>
 */
public class FailSafeValueOf<T extends Enum<T>> implements Serializable {

    /**
     * This will cache a FailSafeValueOf for each enum so that we do not need to recompute a map each time
     */
    private static final Map< Class<? extends Enum<?>> , FailSafeValueOf<? extends Enum<?>> >  CACHE = Maps.newHashMap();


    private final Map<String,T> nameToEnumMap;
    private FailSafeValueOf(Class<T> enumClass) {
        Map<String,T> map = Maps.newHashMap();
        for ( T value : EnumSet.allOf(enumClass)) {
            map.put( value.name() , value);
        }
        this.nameToEnumMap = ImmutableMap.copyOf(map);
    }

    /**
     * Returns the value of the given enum element
     * If the element is not found, null will be returned, and no exception will be thrown
     * @param enumName
     * @return
     */
    public T valueOf(String enumName) {
        return nameToEnumMap.get(enumName);
    }


    /**
     * Get a failsafe value of implementation for a given enum
     * @param enumClass
     * @param <U>
     * @return
     */
    public static <U extends Enum<U>> FailSafeValueOf<U> get(Class<U> enumClass) {
        FailSafeValueOf<U> fsvo = (FailSafeValueOf<U>)CACHE.get(enumClass);
        if ( fsvo == null ) {
            synchronized (FailSafeValueOf.class) {
                fsvo = (FailSafeValueOf<U>)CACHE.get(enumClass);
                if ( fsvo == null ) {
                    fsvo = new FailSafeValueOf<U>(enumClass);
                    CACHE.put(enumClass,fsvo);
                }
            }
        }
        return fsvo;
    }

}

Because i don't want the (little) overhead of creating a new FailSafeValueOf at each access, i've made a cache that keeps for each enum already accessed an already built FailSafeValueOf instance.

I'm not used to handle concurrency. A concurrent access may not be a big problem in such a case as FailSafeValueOf is immutable and 2 different instances of FailSafeValueOf can be returned by the get method for a same enum. But i'd like to know if my implementation of thread-safety is the way to do, and if it's really thread-safe? (mostly for learning purpose)

I don't want to make my method synchronized because after some time, all FailSafeValueOf are created in the cache, and there is no need to forbid concurrent threads to enter the get method.

So what i've made is to check first if there's a cache miss, and then create a synchronized block that will atomically: check again the cache and eventually create the instance. Is it thread-safe and the way to do for such a need?

By the way, enums often have a small number of values. In such a case, is the HashMap an appropriate structure? Is it faster to iterate over the EnumSet and get the appropriate value, instead of using a cache?


Edit:

Please notice that my class is not so useful because the Guava team has releated a method Enums.getIfPresent() which returns an Optional

Your class is not thread-safe because you are not synchronizing around the CACHE.get() method. This assumes that Maps.newHashMap() returns a HashMap and not a ConcurrentHashMap class. You can see this in this code snippet:

    // you need to move your synchronized block out to here
    FailSafeValueOf<U> fsvo = (FailSafeValueOf<U>)CACHE.get(enumClass);
    if ( fsvo == null ) {
        synchronized (FailSafeValueOf.class) {
           ...
           CACHE.put(enumClass,fsvo);
        }
    }

You would need to move the synchronized around the CACHE.get(...) or switch to using a ConcurrentHashMap if that method is called frequently. The problem is that the HashMap would be updated by another thread while the current thread is reading from it -- this can easily cause problems because of race conditions.

Although slightly different, you should also look into the class "Double Check Locking" documentation to understand more the difficulties about trying to save yourself from synchronization like this.

Lastly, I would synchronize on the CACHE object instead of the class which is not recommended unless you really need to have that lock granularity.

This is problematic:

synchronized (FailSafeValueOf.class)

You should only synchronize on private members. The class object is publicly accessible, and other code might choose to lock on it as well, leading to potential deadlock.

Also, the correct solution to allow concurrent get is a reader-writer lock, not skipping the lock. If your current code reads while writing you could get all kinds of grief.

I'd use

private static final Cache<Class<?>, FailSafeValueOf<?>> CACHE
    = CacheBuilder.newBuilder().build(
        new CacheLoader<Class<?>, FailSafeValueOf<?>>() {
            @Override
            public FailSafeValueOf<?> load(Class<?> key) throws Exception {
                return new FailSafeValueOf(key);
            }
    });

which gives you the most flexibility for the least work. Moreover, you can be pretty sure that it works correctly and fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM