ConcurrentHashMap与HashTable

  • ConcurrentHashMap通过CAS锁实现,使用了synchonized(node)搜索链表,只有输入操作有锁,输出操作无锁
  • HashTable通过synchronized方法实现,也就是用this作为锁,粒度过大

ConcurrentHashMap在开源项目的使用

  • Mybatis中执行动态SQL时缓存表达式(expressionCache)

CopyOnWriteHashMap在开源项目的使用

  • zk中的zkNode节点
  • tomcat中的ApplicationContext,即request.getServletContext().getAttribute("key")

Guava中的LocalCache