Java ConcurrentHashMap<;字符串,参考>;vs原子参考<;地图<;字符串,参考>&燃气轮机;

Java ConcurrentHashMap<;字符串,参考>;vs原子参考<;地图<;字符串,参考>&燃气轮机;,java,java.util.concurrent,Java,Java.util.concurrent,选择什么更好?为什么?该地图用作临时存储。它将项目保留一段时间,然后刷新到db 下面是我用原子引用实现的伪代码: public class Service { private final AtomicReference<Map<String, Entity>> storedEntities = new AtomicReference<>(new HashMap<>()); private final AtomicReference&

选择什么更好?为什么?该地图用作临时存储。它将项目保留一段时间,然后刷新到db

下面是我用原子引用实现的伪代码:

public class Service {
    private final AtomicReference<Map<String, Entity>> storedEntities = new AtomicReference<>(new HashMap<>());
    private final AtomicReference<Map<String, Entity>> newEntities = new AtomicReference<>(new HashMap<>());

    private final Dao dao;

    public Service(Dao dao) {
        this.dao = dao;
    }

    @Transactional
    @Async
    public CompletableFuture<Void> save() {
        Map<String, Entity> map = newEntities.getAndSet(new HashMap<>());
        return dao.saveAsync(map.values());
    }

    @Transactional(readOnly = true)
    @Async
    public CompletableFuture<Map<String, Entity>> readAll() {
        return dao.getAllAsync().thenApply(map -> {
            storedEntities.set(map);
            return map;
        });
    }

    @Scheduled(cron = "${cron}")
    public void refreshNow() {
        save();
        readAll();
    }

    public void addNewentity(Entity entity) {
        newEntities.getAndUpdate(map -> {
            map.put(entity.getHash(), entity);
            return map;
        });
    }

    public AtomicReference<List<Entity>> getStoredEntities() {
        return storedEntities.get().values();
    }

    public AtomicReference<List<Entity>> getNewEntities() {
        return newEntities.get().values();
    }
}
公共类服务{
private final AtomicReference storedenties=新的AtomicReference(new HashMap());
private final AtomicReference newEntities=new AtomicReference(new HashMap());
私人最终道道;
公共服务(道道){
this.dao=dao;
}
@交易的
@异步的
公共CompletableFuture save(){
Map Map=newEntities.getAndSet(newHashMap());
返回dao.saveAsync(map.values());
}
@事务(只读=真)
@异步的
公共CompletableFuture readAll(){
返回dao.getAllAsync().thenApply(映射->{
storedEntities.set(地图);
返回图;
});
}
@已计划(cron=“${cron}”)
公众假期(现在){
save();
readAll();
}
public void addNewentity(实体){
newEntities.getAndUpdate(映射->{
map.put(entity.getHash(),entity);
返回图;
});
}
公共原子引用getStoredEntities(){
返回storedEntities.get().values();
}
公共原子引用getNewEntities(){
返回newEntities.get().values();
}
}

正如我所说的,我只需要将数据保存一段时间,然后通过cron将其刷新到db。我对什么是更好的方法感兴趣-AR与CHM?

我首先假设您需要跨多个线程共享访问某些资源,即临时存储映射

简短的回答:

使用ConcurrentHashMap

长(er)答案:

如果您的期望是通过使用原子引用,您将获得一致或线程安全的映射视图,或者您的操作将以原子方式执行,那么您很可能是不正确的-但是,由于您没有提供使用示例,我不能完全肯定地这样说

虽然不能忽略,但性能不应该是您关注的主要问题-相反,您应该确保程序能够正确地按预期执行,然后寻求改进其性能

如果有多个线程读取和/或写入临时存储映射,则映射的内部状态保持一致和正确是很重要的;您可以通过以线程安全的方式实现您的操作来实现这一点,在某种程度上,由包装的或映射都可以实现这一点。然而,上述每种方法都以不同的粒度实现了确保这一点的方法,并且由于ConcurrentHashMap采用了专门的(乐观的)方法,与包装映射的幼稚(悲观的)方法相反,ConcurrentHashMap不太容易受到资源争用的影响,而且可能是两者中表现最好的

前进

我已经提供了下面这篇文章中讨论的三种机制的实现;除了JavaAPI文档之外,还可以浏览下面的代码

import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ConcurrencyExample {

    interface TemporaryStorage<K, V> {

        V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper);

        V put(K key, V value);

        V get(K key);

        void clear();

        @FunctionalInterface
        interface UnitTest<K, V> {

            void test(TemporaryStorage<K, V> store, K key);

        }

    }

    static class ConcurrentHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final Map<K, V> map = new ConcurrentHashMap<>();

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            return map.compute(key, remapper);
        }

        @Override
        public V put(K key, V value) {
            return map.put(key, value);
        }

        @Override
        public V get(K key) {
            return map.get(key);
        }

        @Override
        public void clear() {
            map.clear();
        }
    }

    static class AtomicReferenceHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final AtomicReference<Map<K, V>> map = new AtomicReference<>(new HashMap<>());

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            return map.get().compute(key, remapper);
        }

        @Override
        public V put(K key, V value) {
            return map.get().put(key, value);
        }

        @Override
        public V get(K key) {
            return map.get().get(key);
        }

        @Override
        public void clear() {
            map.get().clear();
        }
    }

    static class MonitorLockedHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final Map<K, V> map = new HashMap<>();
        private final Object mutex = new Object(); // could use the map as the mutex

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            synchronized (mutex) {
                return map.compute(key, remapper);
            }
        }

        @Override
        public V put(K key, V value) {
            synchronized (mutex) {
                return map.put(key, value);
            }
        }

        @Override
        public V get(K key) {
            synchronized (mutex) {
                return map.get(key);
            }
        }

        @Override
        public void clear() {
            synchronized (mutex) {
                map.clear();
            }
        }
    }

    static class WrappedHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final Map<K, V> map = Collections.synchronizedMap(new HashMap<>());

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            return map.compute(key, remapper);
        }

        @Override
        public V put(K key, V value) {
            return map.put(key, value);
        }

        @Override
        public V get(K key) {
            return map.get(key);
        }

        @Override
        public void clear() {
            map.clear();
        }
    }

    static class AtomicUnitTest implements TemporaryStorage.UnitTest<Integer, Integer> {

        @Override
        public void test(TemporaryStorage<Integer, Integer> store, Integer key) {
            store.compute(key, (k, v) -> (v == null ? 0 : v) + 1);
        }
    }

    static class UnsafeUnitTest implements TemporaryStorage.UnitTest<Integer, Integer> {

        @Override
        public void test(TemporaryStorage<Integer, Integer> store, Integer key) {
            Integer value = store.get(key);
            store.put(key, (value == null ? 0 : value) + 1);
        }
    }

    public static class TestRunner {

        public static void main(String... args) throws InterruptedException {
            final int iterations = 1_000;
            final List<Integer> keys = IntStream.rangeClosed(1, iterations).boxed().collect(Collectors.toList());

            final int expected = iterations;

            for (int batch = 1; batch <= 5; batch++) {

                System.out.println(String.format("--- START BATCH %d ---", batch));

                test(System.out, new ConcurrentHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new ConcurrentHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                test(System.out, new AtomicReferenceHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new AtomicReferenceHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                test(System.out, new MonitorLockedHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new MonitorLockedHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                test(System.out, new WrappedHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new WrappedHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                System.out.println(String.format("--- END   BATCH %d ---", batch));

                System.out.println();

            }
        }

        private static <K, V> void test(PrintStream printer, TemporaryStorage<K, V> store, TemporaryStorage.UnitTest<K, V> work, List<K> keys, V expected, int iterations) throws InterruptedException {
            test(printer, store, work, keys, expected, iterations, Runtime.getRuntime().availableProcessors() * 4);
        }

        private static <K, V> void test(PrintStream printer, TemporaryStorage<K, V> store, TemporaryStorage.UnitTest<K, V> work, List<K> keys, V expected, int iterations, int parallelism) throws InterruptedException {
            final ExecutorService workers = Executors.newFixedThreadPool(parallelism);
            final long start = System.currentTimeMillis();

            for (K key : keys) {
                for (int iteration = 1; iteration <= iterations; iteration++) {

                    workers.execute(() -> {
                        try {
                            work.test(store, key);
                        } catch (Exception e) {
                            //e.printStackTrace(); //thrown by the AtomicReference<Map<K, V>> implementation
                        }
                    });

                }
            }

            workers.shutdown();
            workers.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

            final long finish = System.currentTimeMillis();

            final DecimalFormat formatter = new DecimalFormat("###,###");

            final long correct = keys.stream().filter(key -> expected.equals(store.get(key))).count();

            printer.println(String.format("Store '%s' performed %s iterations of %s across %s threads in %sms. Accuracy: %d / %d (%4.2f percent)", store.getClass().getSimpleName(), formatter.format(iterations), work.getClass().getSimpleName(), formatter.format(parallelism), formatter.format(finish - start), correct, keys.size(), ((double) correct / keys.size()) * 100));
        }

    }

}
导入java.io.PrintStream;
导入java.text.DecimalFormat;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.concurrent.ConcurrentHashMap;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.TimeUnit;
导入java.util.concurrent.AtomicReference;
导入java.util.function.BiFunction;
导入java.util.stream.collector;
导入java.util.stream.IntStream;
公共类并发性示例{
接口临时存储器{

V compute(K键,双功能首先,我假设您需要跨多个线程共享访问某些资源,即临时存储映射

简短的回答:

使用ConcurrentHashMap

长(er)答案:

如果您的期望是通过使用原子引用,您将获得一致或线程安全的映射视图,或者您的操作将以原子方式执行,那么您很可能是不正确的-但是,由于您没有提供使用示例,我不能完全肯定地这样说

虽然不能忽略,但性能不应该是您关注的主要问题-相反,您应该确保程序能够正确地按预期执行,然后寻求改进其性能

如果您有多个线程读取和/或写入临时存储映射,映射的内部状态保持一致和正确是很重要的;您可以通过以线程安全的方式实现操作来实现这一点,在这种情况下,或由包装的映射都将实现这一点。但是,每个上述方法中的一种是在不同的粒度上实现确保这一点的方法,这是ConcurrentHashMap采取的专业(乐观)方法的结果,而不是幼稚(悲观)的方法在包装映射的方法中,ConcurrentHashMap不太容易受到资源争用的影响,并且可能是两者中性能更好的

前进

我已经提供了下面这篇文章中讨论的三种机制的实现;除了JavaAPI文档之外,我还对下面的代码进行了探索

import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ConcurrencyExample {

    interface TemporaryStorage<K, V> {

        V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper);

        V put(K key, V value);

        V get(K key);

        void clear();

        @FunctionalInterface
        interface UnitTest<K, V> {

            void test(TemporaryStorage<K, V> store, K key);

        }

    }

    static class ConcurrentHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final Map<K, V> map = new ConcurrentHashMap<>();

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            return map.compute(key, remapper);
        }

        @Override
        public V put(K key, V value) {
            return map.put(key, value);
        }

        @Override
        public V get(K key) {
            return map.get(key);
        }

        @Override
        public void clear() {
            map.clear();
        }
    }

    static class AtomicReferenceHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final AtomicReference<Map<K, V>> map = new AtomicReference<>(new HashMap<>());

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            return map.get().compute(key, remapper);
        }

        @Override
        public V put(K key, V value) {
            return map.get().put(key, value);
        }

        @Override
        public V get(K key) {
            return map.get().get(key);
        }

        @Override
        public void clear() {
            map.get().clear();
        }
    }

    static class MonitorLockedHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final Map<K, V> map = new HashMap<>();
        private final Object mutex = new Object(); // could use the map as the mutex

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            synchronized (mutex) {
                return map.compute(key, remapper);
            }
        }

        @Override
        public V put(K key, V value) {
            synchronized (mutex) {
                return map.put(key, value);
            }
        }

        @Override
        public V get(K key) {
            synchronized (mutex) {
                return map.get(key);
            }
        }

        @Override
        public void clear() {
            synchronized (mutex) {
                map.clear();
            }
        }
    }

    static class WrappedHashMapTS<K, V> implements TemporaryStorage<K, V> {

        private final Map<K, V> map = Collections.synchronizedMap(new HashMap<>());

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remapper) {
            return map.compute(key, remapper);
        }

        @Override
        public V put(K key, V value) {
            return map.put(key, value);
        }

        @Override
        public V get(K key) {
            return map.get(key);
        }

        @Override
        public void clear() {
            map.clear();
        }
    }

    static class AtomicUnitTest implements TemporaryStorage.UnitTest<Integer, Integer> {

        @Override
        public void test(TemporaryStorage<Integer, Integer> store, Integer key) {
            store.compute(key, (k, v) -> (v == null ? 0 : v) + 1);
        }
    }

    static class UnsafeUnitTest implements TemporaryStorage.UnitTest<Integer, Integer> {

        @Override
        public void test(TemporaryStorage<Integer, Integer> store, Integer key) {
            Integer value = store.get(key);
            store.put(key, (value == null ? 0 : value) + 1);
        }
    }

    public static class TestRunner {

        public static void main(String... args) throws InterruptedException {
            final int iterations = 1_000;
            final List<Integer> keys = IntStream.rangeClosed(1, iterations).boxed().collect(Collectors.toList());

            final int expected = iterations;

            for (int batch = 1; batch <= 5; batch++) {

                System.out.println(String.format("--- START BATCH %d ---", batch));

                test(System.out, new ConcurrentHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new ConcurrentHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                test(System.out, new AtomicReferenceHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new AtomicReferenceHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                test(System.out, new MonitorLockedHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new MonitorLockedHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                test(System.out, new WrappedHashMapTS<>(), new AtomicUnitTest(), keys, expected, iterations);
                test(System.out, new WrappedHashMapTS<>(), new UnsafeUnitTest(), keys, expected, iterations);

                System.out.println(String.format("--- END   BATCH %d ---", batch));

                System.out.println();

            }
        }

        private static <K, V> void test(PrintStream printer, TemporaryStorage<K, V> store, TemporaryStorage.UnitTest<K, V> work, List<K> keys, V expected, int iterations) throws InterruptedException {
            test(printer, store, work, keys, expected, iterations, Runtime.getRuntime().availableProcessors() * 4);
        }

        private static <K, V> void test(PrintStream printer, TemporaryStorage<K, V> store, TemporaryStorage.UnitTest<K, V> work, List<K> keys, V expected, int iterations, int parallelism) throws InterruptedException {
            final ExecutorService workers = Executors.newFixedThreadPool(parallelism);
            final long start = System.currentTimeMillis();

            for (K key : keys) {
                for (int iteration = 1; iteration <= iterations; iteration++) {

                    workers.execute(() -> {
                        try {
                            work.test(store, key);
                        } catch (Exception e) {
                            //e.printStackTrace(); //thrown by the AtomicReference<Map<K, V>> implementation
                        }
                    });

                }
            }

            workers.shutdown();
            workers.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

            final long finish = System.currentTimeMillis();

            final DecimalFormat formatter = new DecimalFormat("###,###");

            final long correct = keys.stream().filter(key -> expected.equals(store.get(key))).count();

            printer.println(String.format("Store '%s' performed %s iterations of %s across %s threads in %sms. Accuracy: %d / %d (%4.2f percent)", store.getClass().getSimpleName(), formatter.format(iterations), work.getClass().getSimpleName(), formatter.format(parallelism), formatter.format(finish - start), correct, keys.size(), ((double) correct / keys.size()) * 100));
        }

    }

}
导入java.io.PrintStream;
导入java.text.DecimalFormat;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.concurrent.ConcurrentHashMap;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concur