JUC编程(一)-集合安全性问题

四.集合安全性问题

ArrayList安全性问题

问题:在并发情况下,ArrayList可能会导致add的内容为空,抛出java.util.ConcurrentModificationException(并发修改异常)。

解决方案:

  • List list = new Vector();(Vector的add方法添加了synchronized锁,该方法会影响性能)
  • 点击查看代码
    public class ListS {
        public static void main(String[] args) {
            List list = new Vector();
            for (int i = 0; i < 100; i++) {
                new Thread(()->{
                    list.add(UUID.randomUUID().toString().substring(0,3));
                    System.out.println(list);
                }).start();
            }
        }
    }
    
  • List list = Collections.synchronizedList(new ArrayList());
  • 点击查看代码
    public class ListS {
        public static void main(String[] args) {
            List list = Collections.synchronizedList(new ArrayList());
            for (int i = 0; i < 100; i++) {
                new Thread(()->{
                    list.add(UUID.randomUUID().toString().substring(0,3));
                    System.out.println(list);
                }).start();
            }
        }
    }
  • List list = new CopyOnWriteArrayList();
  • 点击查看代码
    public class ListS {
        public static void main(String[] args) {
            List list = new CopyOnWriteArrayList();
            for (int i = 0; i < 100; i++) {
                new Thread(()->{
                    list.add(UUID.randomUUID().toString().substring(0,3));
                    System.out.println(list);
                }).start();
            }
        }
    }

Set安全性问题

解决方案:

  • Set set = new CopyOnWriteArraySet();
  • 点击查看代码
    public class SetS {
        public static void main(String[] args) {
            Set set = new CopyOnWriteArraySet();
            for (int i = 0; i < 150; i++) {
                new Thread(()->{
                    set.add(UUID.randomUUID().toString().substring(0,3));
                    System.out.println(set);
                }).start();
            }
        }
    }
  • Set set = Collections.synchronizedSet(new HashSet());

Map安全性问题

解决方案:

  • Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
  • 点击查看代码
    public class MapS {
        public static void main(String[] args) {
            Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
            for (int i = 0; i < 100; i++) {
                new Thread(()->{
                    map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,3));
                    System.out.println(map);
                }).start();
            }
        }
    }
    
  • Map<String,String> map = new ConcurrentHashMap<>();
  • 点击查看代码
    public class MapS {
        public static void main(String[] args) {
            Map<String,String> map = new ConcurrentHashMap<>();
            for (int i = 0; i < 100; i++) {
                new Thread(()->{
                    map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,3));
                    System.out.println(map);
                }).start();
            }
        }
    }
上一篇:Java——JUC高并发编程,面试必问( 集合的线程安全)


下一篇:黑马并发编程JUC(信号量、线程安全类)总结