Map m = Collections.synchronizedMap(new HashMap());

 Set s = m.keySet();  // Needn't be in synchronized block

 synchronized(m) {  // Synchronizing on m, not s!

     Iterator i = s.iterator(); // Must be in synchronized block

     while (i.hasNext())

         foo(i.next());

 }

以前一直以为 Collections.synchronizedMap 获取的map是一个同步的不用再手动加锁的,但是今天发现 这个类同步的集合获取iterator的时候 源码中是没有加锁的.....

就是说 synchronizedMap  中的方法并不是所有的操作都是线程安全的


Collections.synchronizedMap(new HashMap())的问题

相关文章:

  • 2021-10-24
  • 2021-08-25
  • 2022-12-23
  • 2021-10-22
  • 2022-01-21
  • 2021-08-26
  • 2022-01-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-11-26
  • 2021-09-21
  • 2022-12-23
  • 2022-02-19
相关资源
相似解决方案