好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中Map实现线程安全的3种方式

方式1.  使用Hashtable

?

1

Map<String,Object> hashtable= new Hashtable<String,Object>();

这是所有人最先想到的,那为什么它是线程安全的?那就看看它的源码,我们可以看出我们常用的put,get,containsKey等方法都是同步的,所以它是线程安全的

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

public synchronized boolean containsKey(Object key) {

        Entry<?,?> tab[] = table;

        int hash = key.hashCode();

        int index = (hash & 0x7FFFFFFF ) % tab.length;

        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {

            if ((e.hash == hash) && e.key.equals(key)) {

                return true ;

            }

        }

        return false ;

    }

 

  public synchronized V get(Object key) {

        Entry<?,?> tab[] = table;

        int hash = key.hashCode();

        int index = (hash & 0x7FFFFFFF ) % tab.length;

        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {

            if ((e.hash == hash) && e.key.equals(key)) {

                return (V)e.value;

            }

        }

        return null ;

    }

      public synchronized V put(K key, V value) {

        // Make sure the value is not null

        if (value == null ) {

            throw new NullPointerException();

        }

 

        // Makes sure the key is not already in the hashtable.

        Entry<?,?> tab[] = table;

        int hash = key.hashCode();

        int index = (hash & 0x7FFFFFFF ) % tab.length;

        @SuppressWarnings ( "unchecked" )

        Entry<K,V> entry = (Entry<K,V>)tab[index];

        for (; entry != null ; entry = entry.next) {

            if ((entry.hash == hash) && entry.key.equals(key)) {

                V old = entry.value;

                entry.value = value;

                return old;

            }

        }

 

        addEntry(hash, key, value, index);

        return null ;

    }

其实现原理是在增删改查的方法上使用了synchronized锁机制,在多线程环境下,无论是读数据,还是修改数据,在同一时刻只能有一个线程在执行synchronize方法,因为是对整个表进行锁定。所以线程越多,对该map的竞争越激烈,效率越低,不推荐使用。

方式2.  使用Collections.synchronizedMap(new Hashtable())

其实现原理是使用工具类里面的静态方法,把传入进来的Hashtable包装成同步的,即在增删改查的方法上增加了synchronized所机制,其实现方式与Hashtable差不多,效率也差不多,不推荐使用。

?

1

Map map = Collections.synchronizedMap( new Hashtable());

以下是JDK源码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {

         return new SynchronizedMap<>(m);

}

private static class SynchronizedMap<K,V>

         implements Map<K,V>, Serializable {

         private static final long serialVersionUID = 1978198479659022715L;

 

         private final Map<K,V> m;     // Backing Map

         final Object      mutex;        // Object on which to synchronize

 

         SynchronizedMap(Map<K,V> m) {

             this .m = Objects.requireNonNull(m);

             mutex = this ;

         }

 

         SynchronizedMap(Map<K,V> m, Object mutex) {

             this .m = m;

             this .mutex = mutex;

         }

 

         public int size() {

             synchronized (mutex) { return m.size();}

         }

         public boolean isEmpty() {

             synchronized (mutex) { return m.isEmpty();}

         }

         public boolean containsKey(Object key) {

             synchronized (mutex) { return m.containsKey(key);}

         }

         public boolean containsValue(Object value) {

             synchronized (mutex) { return m.containsValue(value);}

         }

         public V get(Object key) {

             synchronized (mutex) { return m.get(key);}

         }

 

         public V put(K key, V value) {

             synchronized (mutex) { return m.put(key, value);}

         }

         public V remove(Object key) {

             synchronized (mutex) { return m.remove(key);}

         }

         public void putAll(Map<? extends K, ? extends V> map) {

             synchronized (mutex) {m.putAll(map);}

         }

         public void clear() {

             synchronized (mutex) {m.clear();}

         }

         ......

     }

方式3.  使用ConcurrentHashMap

        其实现原理是Hashtable是对整个表进行加锁,而ConcurrentHashMap是把表进行分段,初始情况下分成16段,每一段都有一把锁,当多个线程访问不同的段时,因为获取到的锁是不同的,所以可以并行的访问。效率比Hashtable高多了,推荐使用。

到此这篇关于Java中Map实现线程安全的3种方式的文章就介绍到这了,更多相关Java Map线程安全内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_42812598/article/details/90708472

查看更多关于Java中Map实现线程安全的3种方式的详细内容...

  阅读:11次