关键词搜索

源码搜索 ×
×

Java之通过Collections.synchronizedMap创建线程安全的HashMap

发布2019-12-21浏览2304次

详情内容

1 问题

我们知道hashMap线程是不安全的,一般而言,我们怎么创建线程安全的HashMap呢?

 

 

 

 

 

2 解决办法

我们可以使用Collections.synchronizedMap来创建HashMap,如下

static Map<String, String> results = Collections.synchronizedMap(new HashMap<String, String>());

 

 

 

 

 

 

3 Collections.synchronizedMap源码部分实现

我们先看synchronizedMap如果创建

  1. public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
  2. return new SynchronizedMap<K,V>(m);
  3. }

我们看下具体的SynchronizedMap类的部分

  1. private static class SynchronizedMap<K,V>
  2. implements Map<K,V>, Serializable {
  3. // use serialVersionUID from JDK 1.2.2 for interoperability
  4. private static final long serialVersionUID = 1978198479659022715L;
  5. private final Map<K,V> m; // Backing Map
  6. final Object mutex; // Object on which to synchronize
  7. SynchronizedMap(Map<K,V> m) {
  8. if (m==null)
  9. throw new NullPointerException();
  10. this.m = m;
  11. mutex = this;
  12. }
  13. SynchronizedMap(Map<K,V> m, Object mutex) {
  14. this.m = m;
  15. this.mutex = mutex;
  16. }
  17. public int size() {
  18. synchronized(mutex) {return m.size();}
  19. }
  20. public boolean isEmpty(){
  21. synchronized(mutex) {return m.isEmpty();}
  22. }
  23. public boolean containsKey(Object key) {
  24. synchronized(mutex) {return m.containsKey(key);}
  25. }
  26. public boolean containsValue(Object value){
  27. synchronized(mutex) {return m.containsValue(value);}
  28. }
  29. public V get(Object key) {
  30. synchronized(mutex) {return m.get(key);}
  31. }
  32. public V put(K key, V value) {
  33. synchronized(mutex) {return m.put(key, value);}
  34. }
  35. public V remove(Object key) {
  36. synchronized(mutex) {return m.remove(key);}
  37. }
  38. public void putAll(Map<? extends K, ? extends V> map) {
  39. synchronized(mutex) {m.putAll(map);}
  40. }
  41. public void clear() {
  42. synchronized(mutex) {m.clear();}
  43. }
  44. private transient Set<K> keySet = null;
  45. private transient Set<Map.Entry<K,V>> entrySet = null;
  46. private transient Collection<V> values = null;
  47. public Set<K> keySet() {
  48. synchronized(mutex) {
  49. if (keySet==null)
  50. keySet = new SynchronizedSet<K>(m.keySet(), mutex);
  51. return keySet;
  52. }
  53. }
  54. public Set<Map.Entry<K,V>> entrySet() {
  55. synchronized(mutex) {
  56. if (entrySet==null)
  57. entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex);
  58. return entrySet;
  59. }
  60. }
  61. public Collection<V> values() {
  62. synchronized(mutex) {
  63. if (values==null)
  64. values = new SynchronizedCollection<V>(m.values(), mutex);
  65. return values;
  66. }
  67. }
  68. public boolean equals(Object o) {
  69. if (this == o)
  70. return true;
  71. synchronized(mutex) {return m.equals(o);}
  72. }
  73. public int hashCode() {
  74. synchronized(mutex) {return m.hashCode();}
  75. }
  76. public String toString() {
  77. synchronized(mutex) {return m.toString();}
  78. }
  79. private void writeObject(ObjectOutputStream s) throws IOException {
  80. synchronized(mutex) {s.defaultWriteObject();}
  81. }
  82. }

SynchronizedMap 实现了Map接口的代理类,该类中对Map接口中的方法还是使用synchronized 同步关键字来保证对Map的操作是线程安全的

synchronized(mutex) {/*****/}

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载