1
  1. これはマルチスレッド化されています
  2. 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 2016 年 4 月 13:47:06,661 DEBUG [WirePushNotificationSyncHandler::WirePushNotification.Worker-1] - eventsForThisCategory は {FX_WIRE_DATA=主要経済データ、ALL_FX_WIRE=すべて、ALL_FX_WIRE=すべて、FX_WIRE_HEADLINES=重要な見出し}

それはどのように可能ですか?

4

2 に答える 2

0

HashMap は、マルチスレッドでは threadSafety ではありません。Collections.synchronizedMap()hashMap インスタンスをラップするために使用するか、ConcurrentHashMapおそらくより良いものを使用することができます

于 2016-04-08T08:50:39.497 に答える