Use ConcurrentHashMap instead of HashMap

This commit is contained in:
Frank van der Heijden 2021-06-28 22:28:10 +02:00
parent 104e086bca
commit 123e2f546c
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
4 changed files with 8 additions and 8 deletions

View file

@ -9,8 +9,8 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Reflection { public class Reflection {
@ -21,7 +21,7 @@ public class Reflection {
public Reflection(Class<?> clazz) { public Reflection(Class<?> clazz) {
this.clazz = clazz; this.clazz = clazz;
this.fieldMap = new HashMap<>(); this.fieldMap = new ConcurrentHashMap<>();
this.methodTree = new NamedReflectionCacheTree<>(); this.methodTree = new NamedReflectionCacheTree<>();
this.constructorTree = new ReflectionCacheTree<>(null); this.constructorTree = new ReflectionCacheTree<>(null);
} }

View file

@ -1,13 +1,13 @@
package dev.frankheijden.minecraftreflection.cache; package dev.frankheijden.minecraftreflection.cache;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public class NamedReflectionCacheTree<T> { public class NamedReflectionCacheTree<T> {
private final Map<String, ReflectionCacheTree<T>> treeMap = new HashMap<>(); private final Map<String, ReflectionCacheTree<T>> treeMap = new ConcurrentHashMap<>();
public T computeIfAbsent(String name, Class<?>[] parameters, Function<Class<?>[], ? extends T> mappingFunction) { public T computeIfAbsent(String name, Class<?>[] parameters, Function<Class<?>[], ? extends T> mappingFunction) {
return treeMap.computeIfAbsent(name, k -> new ReflectionCacheTree<>(null)).computeIfAbsent(parameters, mappingFunction); return treeMap.computeIfAbsent(name, k -> new ReflectionCacheTree<>(null)).computeIfAbsent(parameters, mappingFunction);

View file

@ -2,12 +2,12 @@ package dev.frankheijden.minecraftreflection.cache;
import dev.frankheijden.minecraftreflection.MinecraftReflection; import dev.frankheijden.minecraftreflection.MinecraftReflection;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ReflectionCache { public class ReflectionCache {
private static final Map<Class<?>, MinecraftReflection> cachedReflections = new HashMap<>(); private static final Map<Class<?>, MinecraftReflection> cachedReflections = new ConcurrentHashMap<>();
public static Map<Class<?>, MinecraftReflection> getCachedReflections() { public static Map<Class<?>, MinecraftReflection> getCachedReflections() {
return cachedReflections; return cachedReflections;

View file

@ -1,7 +1,7 @@
package dev.frankheijden.minecraftreflection.cache; package dev.frankheijden.minecraftreflection.cache;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -10,7 +10,7 @@ public class ReflectionCacheTree<T> {
public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0]; public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
private T value; private T value;
private final Map<Class<?>, ReflectionCacheTree<T>> children = new HashMap<>(); private final Map<Class<?>, ReflectionCacheTree<T>> children = new ConcurrentHashMap<>();
public ReflectionCacheTree(T value) { public ReflectionCacheTree(T value) {
this.value = value; this.value = value;