zoukankan      html  css  js  c++  java
  • 5. 集合不安全

    List 不安全

    List<String> list = new ArrayList<>();
    
            List<String> list2 = Collections.synchronizedList(new ArrayList<>());
    
            // CopyOnWrite 写入时复制;
            /**
             * CopyOnWriteArrayList 比 Vetor 厉害在:
             * CopyOnWriteArrayList 用的 Lock 锁, Vetor 使用的是 synchronized 锁,synchronized 锁性能没有Lock好
             *
             *
             * 多个线程调用的时候,list ,读取的时候,固定的,写入
             * 在写入的时候避免覆盖,造成数据问题。
             * 读写分离
             */
            List<String> list3 = new CopyOnWriteArrayList<>();
    
            for (int i = 1; i <=100 ; i++) {
                new Thread(()->{
                    list3.add("1");
                    System.out.println(list3);
                }, String.valueOf(i)).start();
            }
    
    

    Set

            Set<String> set = new HashSet<>();
    
            Set<String> set2 = Collections.synchronizedSet(new HashSet<>());
    
            Set<String> set3 = new CopyOnWriteArraySet<>();
    

    HashSet 的底层

        public HashSet() {
            map = new HashMap<>();
        }
    
        // set 的本质就是 map 的key,因为key是无法重复的
        public boolean add(E e) {
            return map.put(e, PRESENT)==null;
        }
    

    Map 不安全

        Map<String, Object> map = new HashMap<>();
    
        // 1.map 是这样用的吗
        /**
         * 工作中不用 HashMap
         */
    
        // 2.默认等价于什么?
        /**
         * Map<String, Object> map = new HashMap<>();
         * ===  Map<String, Object> map = new HashMap<>(16, 0.75);
         */
    
        /**
         * 加载因子和初始化容量
         */
        Map<String, Object> map1 = new ConcurrentHashMap<>();
    
  • 相关阅读:
    织梦系统的安装与详细信息
    js 报错 :object is not a function
    css3中动画animation的应用
    js 中 setTimeout()的用法
    CSS3 Gradient-CSS3渐变
    css3 transform 变形
    CSS3 transition 属性
    只要有人的地方,世界就不是冰冷的。
    CSS层
    css区块定位之浮动与清除属性
  • 原文地址:https://www.cnblogs.com/blackBlog/p/13451428.html
Copyright © 2011-2022 走看看