修改 SafeConcurrentHashMap 的错误。

feature/net-util
ZhouXY108 2023-08-06 03:43:44 +08:00
parent ad68cf2e3d
commit 4fc72f3d5e
2 changed files with 82 additions and 5 deletions

View File

@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.base;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
/**
* JRE version
*/
public class JRE {
private static final int JAVA_8 = 8;
public static final int CURRENT_VERSION = getJre();
public static boolean isJava8() {
return CURRENT_VERSION == JAVA_8;
}
private static int getJre() {
String version = System.getProperty("java.version");
boolean isBlank = StringUtils.isBlank(version);
if (!isBlank && version.startsWith("1.8")) {
return JAVA_8;
}
// if JRE version is 9 or above, we can get version from
// java.lang.Runtime.version()
try {
Object javaRunTimeVersion = MethodUtils.invokeMethod(Runtime.getRuntime(), "version");
return (int) MethodUtils.invokeMethod(javaRunTimeVersion, "major");
} catch (Exception e) {
// Can't determine current JRE version (maybe java.version is null),
// assuming that JRE version is 8.
}
// default java 8
return JAVA_8;
}
private JRE() {
throw new IllegalStateException("Utility class");
}
}

View File

@ -1,11 +1,14 @@
package xyz.zhouxy.plusone.commons.collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import javax.annotation.concurrent.ThreadSafe;
import xyz.zhouxy.plusone.commons.base.JRE;
@ThreadSafe
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
@ -83,11 +86,27 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
/** {@inheritDoc} */
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
V v = get(key);
if (null == v) {
putIfAbsent(key, mappingFunction.apply(key));
v = get(key);
Objects.requireNonNull(mappingFunction);
if (JRE.isJava8()) {
V v = get(key);
if (null == v) {
// this bug fix methods maybe cause `mappingFunction.apply` multiple calls.
v = mappingFunction.apply(key);
if (null == v) {
return null;
}
final V res = putIfAbsent(key, v);
if (null != res) {
// if pre value present, means other thread put value already,
// and putIfAbsent not effect
// return exist value
return res;
}
// if pre value is null, means putIfAbsent effected, return current value
}
return v;
} else {
return computeIfAbsent(key, mappingFunction);
}
return v;
}
}