我正在维护一个内存HashMap,它存储testUserIds了他们的列表emailId.
HashMap<Integer, String> testUsers = new HashMap<>()
Run Code Online (Sandbox Code Playgroud)
并且只有一个后台线程读取列表的添加/删除testUsers并在此映射上执行操作.由于只有一个线程写入此映射,因此我不必将其作为同步映射.
但是有多个线程从这张地图中读取.ConcurrentHashMap如果只有一位作家而且有多位读者,我是否需要?
在我的应用程序中,我需要维护一个存储器HashMap列表userIds以及它们的列表score.
有一个Writer Thread根据业务逻辑更新用户的分数.许多Reader Threads score从这张地图中读取用户,即map.get(userId).
列表userIds是静态的,即没有新用户添加到地图中.
按照JavaDoc If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
我没有进行任何结构改变(没有添加/删除).我是否需要使用ConcurrentHashMap或任何其他Synchronization构造用于此类用例?