【发布时间】:2016-07-29 10:43:09
【问题描述】:
- 这是在多线程下
- 我已阅读有关 HashMap 的主题,但找不到相关问题/答案
代码:
private Map<String, String> eventsForThisCategory;
...
Map<String, String> getEventsForThisCategory() {
if (eventsForThisCategory == null) {
Collection<INotificationEventsObj> notificationEvents = notificationEventsIndex.getCategoryNotificationEvents(getCategoryId());
eventsForThisCategory = new HashMap<String, String>();
for(INotificationEventsObj notificationEvent : notificationEvents) {
eventsForThisCategory.put(notificationEvent.getNotificationEventID(), notificationEvent.getNotificationEventName());
}
logger.debug("eventsForThisCategory is {}", eventsForThisCategory);
}
return eventsForThisCategory;
}
输出:
app_debug.9.log.gz:07 Apr 2016 13:47:06,661 DEBUG [WirePushNotificationSyncHandler::WirePushNotification.Worker-1] - eventsForThisCategory 是 {FX_WIRE_DATA=关键经济数据, ALL_FX_WIRE=All, ALL_FX_WIRE=All, FX_WIRE_HEADLINES=重要标题}
这怎么可能?
【问题讨论】:
-
嗯,这是不可能的。至少不是
HashMap<String, String>。你的代码有什么样的并发性?两个ALL_FX_WIRE键之间是否存在不同的空格? -
@Kayaman 是多线程的。没有空格可以区分这两个键。如您所见,它打印出地图,我不相信它会自动修剪字符串
-
@YuriBlanc 我会更新问题,我已经阅读了该主题,对 HashMap 的工作原理有基本的了解,甚至知道 HashMap 不是线程安全的。
-
嗯,唯一想到的是并发问题。我假设您无法可靠地复制该行为?
标签: java hashmap duplicates multiple-entries