ConcurrentHashMap的简要总结

一.ConcurrentHashMap的简要总结

1、public V get(Object key)不涉及到锁,也就是说获得对象时没有使用锁;

2、put、remove方法要使用锁,但并不一定有锁争用,原因在于ConcurrentHashMap将缓存的变量分到多个Segment,每个Segment上有一个锁,只要多个线程访问的不是一个Segment就没有锁争用,就没有堵塞,各线程用各自的锁,ConcurrentHashMap缺省情况下生成16个Segment,也就是允许16个线程并发的更新而尽量没有锁争用;

3、Iterator对象的使用,不一定是和其它更新线程同步,获得的对象可能是更新前的对象,

ConcurrentHashMap允许一边更新、一边遍历,也就是说在Iterator对象遍历的时候,ConcurrentHashMap也可以进行remove,put操作,且遍历的数据会随着remove,put操作产出变化

所以希望遍历到当前全部数据的话,要么以ConcurrentHashMap变量为锁进行同步(synchronized该变量),要么使用CopiedIterator包装iterator,使其拷贝当前集合的全部数据,但是这样生成的iterator不可以进行remove操作。

二.Hashtable和ConcurrentHashMap的不同点:

1、Hashtable对get,put,remove都使用了同步操作,它的同步级别是正对Hashtable来进行同步的,也就是说如果有线程正在遍历集合,其他的线程就暂时不能使用该集合了,这样无疑就很容易对性能和吞吐量造成影响,从而形成单点。而ConcurrentHashMap则不同,它只对put,remove操作使用了同步操作,get操作并不影响,详情请看以上第1,2点,当前ConcurrentHashMap这样的做法对一些线程要求很严格的程序来说,还是有所欠缺的,对应这样的程序来说,如果不考虑性能和吞吐量问题的话,个人觉得使用Hashtable还是比较合适的;

2、Hashtable在使用iterator遍历的时候,如果其他线程,包括本线程对Hashtable进行了put,remove等更新操作的话,就会抛出ConcurrentModificationException异常,但如果使用ConcurrentHashMap的话,就不用考虑这方面的问题了,详情请看以上第3点;

 

1.HashMap或者ArrayList边遍历边删除数据会报java.util.ConcurrentModificationException异常

[java]  view plain  copy
  1. <span style="white-space:pre">    </span>Map<Long, String> mReqPacket = new HashMap<Long, String>();  
  2.         for (long i = 0; i < 15; i++) {  
  3.             mReqPacket.put(i, i + "");  
  4.         }  
  5.   
  6.         for (Entry<Long, String> entry : mReqPacket.entrySet()) {  
  7.             long key = entry.getKey();  
  8.             String value = entry.getValue();  
  9.             if (key < 10) {  
  10.                 mReqPacket.remove(key);  
  11.             }  
  12.         }  
  13.   
  14.         for (Entry<Long, String> entry : mReqPacket.entrySet()) {  
  15.             Log.d(entry.getKey() + " " + entry.getValue());  
  16.         }  


所以要用迭代器删除元素:

[java]  view plain  copy
  1. <span style="white-space:pre">    </span>Map<Long, String> mReqPacket = new HashMap<Long, String>();  
  2.         for (long i = 0; i < 15; i++) {  
  3.             mReqPacket.put(i, i + "");  
  4.         }  
  5.   
  6.         for (Iterator<Entry<Long, String>> iterator = mReqPacket.entrySet().iterator(); iterator.hasNext();) {  
  7.             Entry<Long, String> entry = iterator.next();  
  8.             long key = entry.getKey();  
  9.             if (key < 10) {  
  10.                 iterator.remove();  
  11.             }  
  12.         }  
  13.   
  14.         for (Entry<Long, String> entry : mReqPacket.entrySet()) {  
  15.             Log.d(entry.getKey() + " " + entry.getValue());  
  16.         }  


2.对ConcurrentHashMap边遍历边删除或者增加操作不会产生异常(可以不用迭代方式删除元素),因为其内部已经做了维护,遍历的时候都能获得最新的值。即便是多个线程一起删除、添加元素也没问题。

[java]  view plain  copy
  1. <span style="white-space:pre">    </span>Map<Long, String> conMap = new ConcurrentHashMap<Long, String>();  
  2.         for (long i = 0; i < 15; i++) {  
  3.             conMap.put(i, i + "");  
  4.         }  
  5.   
  6.         for (Entry<Long, String> entry : conMap.entrySet()) {  
  7.             long key = entry.getKey();  
  8.             if (key < 10) {  
  9.                 conMap.remove(key);  
  10.             }  
  11.         }  
  12.   
  13.         for (Entry<Long, String> entry : conMap.entrySet()) {  
  14.             Log.d(entry.getKey() + " " + entry.getValue());  
  15.         }  

3.一个线程对ConcurrentHashMap增加数据,另外一个线程在遍历时就能获得。

[java]  view plain  copy
  1. static Map<Long, String> conMap = new ConcurrentHashMap<Long, String>();  
  2. public static void main(String[] args) throws InterruptedException {  
  3.     for (long i = 0; i < 5; i++) {  
  4.         conMap.put(i, i + "");  
  5.     }  
  6.   
  7.     Thread thread = new Thread(new Runnable() {  
  8.         public void run() {  
  9.             conMap.put(100l, "100");  
  10.             Log.d("ADD:" + 100);  
  11.             try {  
  12.                 Thread.sleep(100);  
  13.             } catch (InterruptedException e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.         }  
  17.   
  18.     });  
  19.     Thread thread2 = new Thread(new Runnable() {  
  20.         public void run() {  
  21.             for (Iterator<Entry<Long, String>> iterator = conMap.entrySet().iterator(); iterator.hasNext();) {  
  22.                 Entry<Long, String> entry = iterator.next();  
  23.                 Log.d(entry.getKey() + " - " + entry.getValue());  
  24.                 try {  
  25.                     Thread.sleep(100);  
  26.                 } catch (InterruptedException e) {  
  27.                     e.printStackTrace();  
  28.                 }  
  29.             }  
  30.         }  
  31.     });  
  32.     thread.start();  
  33.     thread2.start();  
  34.   
  35.     Thread.sleep(3000);  
  36.     Log.d("--------");  
  37.     for (Entry<Long, String> entry : conMap.entrySet()) {  
  38.         Log.d(entry.getKey() + " " + entry.getValue());  
  39.     }  
  40.   
  41. }  


输出:
[java]  view plain  copy
  1. ADD:100  
  2. 0 - 0  
  3. 100 - 100  
  4. 2 - 2  
  5. 1 - 1  
  6. 3 - 3  
  7. 4 - 4  
  8. --------  
  9. 0 0  
  10. 100 100  
  11. 2 2  
  12. 1 1  
  13. 3 3  
  14. 4 4  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值