Synchronization in a HashMap cache

前端 未结 4 612
孤独总比滥情好
孤独总比滥情好 2021-01-18 03:03

I\'ve got a web application where people ask for resources. This resources are cached using a synchronized hash map for efficiency. The problem here is when two different re

4条回答
  •  旧时难觅i
    2021-01-18 03:46

    Your code looks ok, except that you are synchronizing more than actually required:

    • Using a ConcurrentHashMap instead of a synchronized HashMap would allow multiple invocations of the get method without locking.

    • Synchronizing on this instead of resources is probably not necessary, but it depends on the rest of your code.

提交回复
热议问题