dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

Add a JRE version utils

Open KamToHung opened this issue 2 years ago • 1 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.
  • [x] I have searched the release notes of this repository and believe that this is not a duplicate.

Add an JRE version utils

public class ConcurrentHashMapUtils {

    private static boolean IS_JAVA8;

    static {
        try {
            IS_JAVA8 = System.getProperty("java.version").startsWith("1.8.");
        } catch (Exception ignore) {
            // exception is ignored
            IS_JAVA8 = true;
        }
    }

    /**
     * A temporary workaround for Java 8 ConcurrentHashMap#computeIfAbsent specific performance issue: JDK-8161372.</br>
     * @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
     *
     */
    public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Function<? super K, ? extends V> func) {
        if (IS_JAVA8) {
            V v = map.get(key);
            if (null == v) {
                v = map.computeIfAbsent(key, func);
            }
            return v;
        } else {
            return map.computeIfAbsent(key, func);
        }
    }

}

Sometimes we should fix some bug with different JRE verison, may I add a JRE version utils?

KamToHung avatar Jan 30 '23 06:01 KamToHung

Pls go ahead

AlbumenJ avatar Jan 30 '23 07:01 AlbumenJ