java - Hashmap jdk1.7无锁get()和同步put()的线程安全

标签 java performance collections thread-safety

场景:一个类使用 Jdk1.7 java.util.HashMap,仅调用 get() 和 put() 方法。我试图避免 get() 方法同步。当必须加载新类时,之前同步的方法 ClassloaderHashMap.get() 可能会阻塞我的所有线程几秒钟。类加载的本质是将对象添加到 HashMap 中并且永远不会删除。我的应用程序使用 400 个线程和 30'000 个类。我无法使用 ConcurrentHashMap。

/**
 * Class to simulate lock free reads from HashMap in WebClassLoader.
 */
public static class ClassloaderHashMap {
    private final HashMap<String, String> testHashMap = new HashMap<String, String>();

    public String get(String key) {
        if (testHashMap.containsKey(key)) {
            String result = testHashMap.get(key);
            if (result != null) {
                return result;
            }
        }
        // call synchronized method
        return writeAndGet(key);
    }

    private synchronized String writeAndGet(String key) {
        // find and load class by key, for the test scenario simply use value=key
        testHashMap.put(key, key);
        return testHashMap.get(key);
    }
}

问题:此解决方案是否存在潜在危险?

我使用以下代码成功测试了多线程场景:

package alex;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

public class PerfTestLockFreeReadHashMap {
    private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
    private static final int KEY_COUNT = 30179; // same number of loaded classes
                                                // as in my app

    private static int NUM_WRITERS = 20;
    private static int NUM_READERS = 400;
    private static long TEST_DURATION_MS = 1000;

    private static final String[] keysArray = new String[KEY_COUNT];
    static {
        for (int i = 0; i < keysArray.length; i++) {
            keysArray[i] = "com.company.SomeClass-" + i;
        }
    }

    /**
     * Class to simulate lock free reads from HashMap in WebClassLoader.
     */
    public static class ClassloaderHashMap {
        private final HashMap<String, String> testHashMap = new HashMap<String, String>();
        private AtomicLong reads = new AtomicLong();
        private AtomicLong nullentries =  new AtomicLong();
        private AtomicLong writes = new AtomicLong();

        public String get(String key) {
            if (testHashMap.containsKey(key)) {
                reads.incrementAndGet();
                String result = testHashMap.get(key);
                if (result != null) {
                    return result;
                } else {
                    nullentries.incrementAndGet();
                }
            }
            // call synchronized method
            return writeAndGet(key);
        }

        public synchronized String writeAndGet(String key) {
            writes.incrementAndGet();
            testHashMap.put(key, key);
            return testHashMap.get(key);
        }

        @Override
        public String toString() {
            return "ClassloaderHashMap [Lock-free reads=" + reads + ", Null entries=" + nullentries +  ", writes=" + writes + "]";
        }

    }

    public static void main(final String[] args) throws Exception {
        for (int i = 0; i < 10; i++) {
            ClassloaderHashMap classloaderHashMap = new ClassloaderHashMap();
            System.out.println("*** Run - " + i);
            perfRun(classloaderHashMap);
            System.out.println(classloaderHashMap);
        }
        EXECUTOR.shutdown();
    }

    public static void perfRun(final ClassloaderHashMap classloaderHashMap) throws Exception {
        final CyclicBarrier startBarrier = new CyclicBarrier(NUM_READERS + NUM_WRITERS + 1);
        final CountDownLatch finishLatch = new CountDownLatch(NUM_READERS + NUM_WRITERS);
        final AtomicBoolean runningFlag = new AtomicBoolean(true);
        for (int i = 0; i < NUM_WRITERS; i++) {
            EXECUTOR.execute(new WriterRunner(classloaderHashMap, i, runningFlag, startBarrier, finishLatch));
        }
        for (int i = 0; i < NUM_READERS; i++) {
            EXECUTOR.execute(new ReaderRunner(classloaderHashMap, i, runningFlag, startBarrier, finishLatch));
        }
        awaitBarrier(startBarrier);
        Thread.sleep(TEST_DURATION_MS);
        runningFlag.set(false);
        finishLatch.await();
        System.out.format("%d readers %d writers \n", NUM_READERS, NUM_WRITERS);
    }

    public static void awaitBarrier(final CyclicBarrier barrier) {
        try {
            barrier.await();
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static class WriterRunner implements Runnable {
        private final int id;
        private final AtomicBoolean runningFlag;
        private final CyclicBarrier barrier;
        private final CountDownLatch latch;
        private final ClassloaderHashMap classloaderHashMap;

        public WriterRunner(final ClassloaderHashMap classloaderHashMap, final int id, final AtomicBoolean runningFlag, final CyclicBarrier barrier,
                final CountDownLatch latch) {
            this.id = id;
            this.runningFlag = runningFlag;
            this.barrier = barrier;
            this.latch = latch;
            this.classloaderHashMap = classloaderHashMap;
        }

        @Override
        public void run() {
            awaitBarrier(barrier);
            int writeCounter = 0;
            while (runningFlag.get()) {
                String key = writeCounter + keysArray[writeCounter % KEY_COUNT] + id;
                String result = classloaderHashMap.get(key);
                if (result == null) {
                    result = classloaderHashMap.writeAndGet(key);
                }

                if (!key.equals(result)) {
                    throw new RuntimeException(String.format("Got %s instead of %s.\n", result, key));
                }
                ++writeCounter;
            }
            latch.countDown();
        }
    }

    public static class ReaderRunner implements Runnable {
        private final int id;
        private final AtomicBoolean runningFlag;
        private final CyclicBarrier barrier;
        private final CountDownLatch latch;
        private final ClassloaderHashMap classloaderHashMap;

        public ReaderRunner(final ClassloaderHashMap classloaderHashMap, final int id, final AtomicBoolean runningFlag, final CyclicBarrier barrier,
                final CountDownLatch latch) {
            this.id = id;
            this.runningFlag = runningFlag;
            this.barrier = barrier;
            this.latch = latch;
            this.classloaderHashMap = classloaderHashMap;
        }

        @Override
        public void run() {
            awaitBarrier(barrier);
            int readCounter = 0;
            while (runningFlag.get()) {
                String key = keysArray[readCounter % keysArray.length] + "-" + id;
                String result = classloaderHashMap.get(key);
                if (result == null) {
                    result = classloaderHashMap.writeAndGet(key);
                }

                if (!key.equals(result)) {
                    throw new RuntimeException(String.format("Got %s instead of %s.\n", result, key));
                }

                ++readCounter;
            }
            latch.countDown();
        }
    }


}

示例输出 - 可能会发生 null 条目,但不会导致错误,在这种情况下调用同步方法:

*** Run - 0
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4288664, Null entries=0, writes=589699]
*** Run - 1
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4177513, Null entries=0, writes=965519]
*** Run - 2
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4701346, Null entries=0, writes=971986]
*** Run - 3
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=8181871, Null entries=1, writes=2076311]
*** Run - 4
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=3225071, Null entries=0, writes=616041]
*** Run - 5
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=2923419, Null entries=0, writes=1762663]
*** Run - 6
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=5514584, Null entries=0, writes=1090732]
*** Run - 7
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4037333, Null entries=0, writes=948106]
*** Run - 8
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=6604630, Null entries=0, writes=750456]
*** Run - 9
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=5263678, Null entries=0, writes=894637]

最佳答案

不,HashMap 不是线程安全的。如果有一个线程写入映射,而另一个线程从中读取,则读取线程可能会看到映射处于不一致状态。当然,这可能会在很长一段时间内正常运行,但随后会产生一个难以重现和发现的错误。

使用同步的 get() 方法时,问题在于对 map 的所有访问都会同步。因此,当两个线程同时尝试从映射中读取时,一个必须等​​待另一个(尽管同时读取不是问题)。对于 400 个线程,这确实可能会导致明显的延迟。

解决您的问题的方法是使用java.util.concurrent.locks.ReadWriteLock。 (Java 为该接口(interface)提供了 java.util.concurrent.locks.ReentrantReadWriteLock 实现。)使用此锁,您可以确保任意数量的线程可以同时对某个对象进行读访问,但只能有一个线程可以写入映射(如果一个线程正在写入,则没有其他线程可以读取)。查看Java API文档,了解如何使用诸如lock之类的内容。

关于java - Hashmap jdk1.7无锁get()和同步put()的线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217437/

相关文章:

java - 如何自动从类数组转换为数组类

android - Kotlin:类型推断失败。预期类型不匹配:推断类型为 MutableList<Long?> 但预期为 MutableCollection<Long>

collections - 为什么这会触发 Backbone Collection 上的 "change"事件?

java - 如何在java类和android Activity之间交换数据?

java - 如何一次将单个数字读取为单个字符而不是字母

java - Android - teamviewer native touch 的工作原理

java - 如何知道一个数字在数组中出现的频率

iphone - Cocos2d : usage of references vs getChild performance

java - 使用 "this.xxx"指向字段时有性能差异吗?

java - 如何在 JPA 中拥有 2 个相同类型的集合?