Collections.synchronizedList使用


1.SynchronizedList類具體代碼:

  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);}
        }

        private Object readResolve() {
            return (list instanceof RandomAccess
                    ? new SynchronizedRandomAccessList<>(list)
                    : this);
        }
    }

1.使用方式

官方文檔就是下面的使用方式

List list = Collections.synchronizedList(new ArrayList());
      ...
  synchronized (list) {
      Iterator i = list.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }

既然封裝類內部已經加了對象鎖,為什么外部還要加一層對象鎖?

看源碼可知,Collections.synchronizedList中很多方法,比如equals,hasCode,get,set,add,remove,indexOf,lastIndexOf......

都添加了鎖,但是List中

Iterator<E> iterator();

這個方法沒有加鎖,不是線程安全的,所以如果要遍歷,還是必須要在外面加一層鎖。

使用Iterator迭代器的話,似乎也沒必要用Collections.synchronizedList的方法來包裝了——反正都是必須要使用Synchronized代碼塊包起來的。

所以總的來說,Collections.synchronizedList這種做法,適合不需要使用Iterator、對性能要求也不高的情況。

2.SynchronizedList和Vector最主要的區別:

  1. Vector擴容為原來的2倍長度,ArrayList擴容為原來1.5倍
  2. SynchronizedList有很好的擴展和兼容功能。他可以將所有的List的子類轉成線程安全的類。
  3. 使用SynchronizedList的時候,進行遍歷時要手動進行同步處理 。
  4. SynchronizedList可以指定鎖定的對象。

3.for的注意點與

for (int i = 0; i < list.size(); i++) {
    System.out.print(list.get(i) + ",");
}

Iterator iterator = list.iterator();
while (iterator.hasNext()) {
    System.out.print(iterator.next() + ",");
}

for (Integer i : list) {
    System.out.print(i + ",");
}

第一種是普通的for循環遍歷、第二種是使用迭代器進行遍歷,第三種我們一般稱之為增強for循環(for each)

可以看到,第三種形式是JAVA提供的語法糖,這里我們剖洗一下,這種增強for循環底層是如何實現的。

   for (Integer i : list) {
       System.out.println(i);
   }

反編譯后:

    Integer i;
    for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i)){
        i = (Integer)iterator.next();        
    }

如果在Vector,Collections.synchronizedList使用增強for循環,就必須在外面單獨加鎖,因為它不是單單一個操作,不是原子性的,如果在遍歷的過程中,進行add,remove操作,就會拋出異常。

參考:

通過Collections.synchronizedList獲取安全的list后,為何還要用synchronized修飾?

SynchronizedList和Vector的區別

【集合類型的並發】Collections.synchronizedList

ArrayList、Vector和Collections.synchronizedList()

Java中的增強for循環(for each)的實現原理與坑


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM