zoukankan      html  css  js  c++  java
  • work_20_对work_19中api的解释

    1.Collections.synchronizedList(new ArrayList<String>());

    java集合工具类Collections.synchronizedList提供了集合的线程安全包装方法。

    初始化一个synchronizedList事首先判断初始化对象是否实现了RandomAccess,构建相应的的SynchronizedRandomAccessList对象或者SynchronizedList对象

    RandomAccess:判断这个集合是否可以快速访问的表示(例如:arrayList的 arrayList.get(1))

    public class ArrayList<E> extends AbstractList<E>
            implements List<E>, RandomAccess, Cloneable, java.io.Serializable
    {}

    Collections.synchronizedList实际是对所有synchronizedList构建的对象的操作添加了synchronized

    listIterator方法必须用户手动添加synchronized

    public static <T> List<T> synchronizedList(List<T> list) {
            return (list instanceof RandomAccess ?
                    new SynchronizedRandomAccessList<>(list) :
                    new SynchronizedList<>(list));
        }
    
        static <T> List<T> synchronizedList(List<T> list, Object mutex) {
            return (list instanceof RandomAccess ?
                    new SynchronizedRandomAccessList<>(list, mutex) :
                    new SynchronizedList<>(list, mutex));
        }
    
        /**
         * @serial include
         */
        static class SynchronizedList<E>
            extends SynchronizedCollection<E>
            implements List<E> {
            private static final long serialVersionUID = -7754090372962971524L;
    
            final List<E> list;
    
            SynchronizedList(List<E> list) {
                super(list);
                this.list = list;
            }
            SynchronizedList(List<E> list, Object mutex) {
                super(list, mutex);
                this.list = list;
            }
    
            public boolean equals(Object o) {
                if (this == o)
                    return true;
                synchronized (mutex) {return list.equals(o);}
            }
            public int hashCode() {
                synchronized (mutex) {return list.hashCode();}
            }
    
            public E get(int index) {
                synchronized (mutex) {return list.get(index);}
            }
            public E set(int index, E element) {
                synchronized (mutex) {return list.set(index, element);}
            }
            public void add(int index, E element) {
                synchronized (mutex) {list.add(index, element);}
            }
            public E remove(int index) {
                synchronized (mutex) {return list.remove(index);}
            }
    
            public int indexOf(Object o) {
                synchronized (mutex) {return list.indexOf(o);}
            }
            public int lastIndexOf(Object o) {
                synchronized (mutex) {return list.lastIndexOf(o);}
            }
    
            public boolean addAll(int index, Collection<? extends E> c) {
                synchronized (mutex) {return list.addAll(index, c);}
            }
    
            public ListIterator<E> listIterator() {
                return list.listIterator(); // Must be manually synched by user
            }
    
            public ListIterator<E> listIterator(int index) {
                return list.listIterator(index); // Must be manually synched by user
            }
    
            public List<E> subList(int fromIndex, int toIndex) {
                synchronized (mutex) {
                    return new SynchronizedList<>(list.subList(fromIndex, toIndex),
                                                mutex);
                }
            }
    
            @Override
            public void replaceAll(UnaryOperator<E> operator) {
                synchronized (mutex) {list.replaceAll(operator);}
            }
            @Override
            public void sort(Comparator<? super E> c) {
                synchronized (mutex) {list.sort(c);}
            }

    list 对象直接维护了传递进来的参数List类型参数而在get set add remove等方法中的实现都用线程同步语句块 synchronized (mutex)封装起来。那么mutex这把锁是谁呢?

    super(list);里面去找了.这时候就来到了SynchronizedList的父类SynchronizedCollection

    mutex 就是自己,也就是直接对自己加锁了

     SynchronizedCollection(Collection<E> c) {
                this.c = Objects.requireNonNull(c);
                mutex = this;
            }

     2.IntStream.range(0,100).parallel()<关于对stream的详解见 work_20>

    使用并行流遍历0-100的数字

    3. AtomicInteger SEQ = new AtomicInteger(1000);

    高并发的情况下,i++无法保证原子性,往往会出现问题,所以引入AtomicInteger类。

      

  • 相关阅读:
    IDEA安装步骤
    记录常用网站
    IDEA解决中文乱码问题
    使用IDEA 中 实现springboot 热部署 (spring boot devtools版)
    Springboot学习
    写文档步骤
    some untracked working tree files问题解决
    利用mybatis-generator自动生成代码
    Java实现打印功能
    sql语句大全
  • 原文地址:https://www.cnblogs.com/asndxj/p/13589358.html
Copyright © 2011-2022 走看看