Collections.synchronizedList使用
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);}}@Overridepublic void replaceAll(UnaryOperator<E> operator) {synchronized (mutex) {list.replaceAll(operator);}}@Overridepublic 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 blockwhile (i.hasNext())foo(i.next());}既然封裝類內(nèi)部已經(jīng)加了對(duì)象鎖,為什么外部還要加一層對(duì)象鎖?
看源碼可知,Collections.synchronizedList中很多方法,比如equals,hasCode,get,set,add,remove,indexOf,lastIndexOf…
都添加了鎖,但是List中
Iterator<E> iterator();這個(gè)方法沒(méi)有加鎖,不是線程安全的,所以如果要遍歷,還是必須要在外面加一層鎖。
使用Iterator迭代器的話,似乎也沒(méi)必要用Collections.synchronizedList的方法來(lái)包裝了——反正都是必須要使用Synchronized代碼塊包起來(lái)的。
所以總的來(lái)說(shuō),Collections.synchronizedList這種做法,適合不需要使用Iterator、對(duì)性能要求也不高的情況。
2.SynchronizedList和Vector最主要的區(qū)別:
3.for的注意點(diǎn)與
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循環(huán)遍歷、第二種是使用迭代器進(jìn)行遍歷,第三種我們一般稱之為增強(qiáng)for循環(huán)(for each)
可以看到,第三種形式是JAVA提供的語(yǔ)法糖,這里我們剖洗一下,這種增強(qiáng)for循環(huán)底層是如何實(shí)現(xiàn)的。
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使用增強(qiáng)for循環(huán),就必須在外面單獨(dú)加鎖,因?yàn)樗皇菃螁我粋€(gè)操作,不是原子性的,如果在遍歷的過(guò)程中,進(jìn)行add,remove操作,就會(huì)拋出異常。
總結(jié)
以上是生活随笔為你收集整理的Collections.synchronizedList使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: springboot基于mybatis扫
- 下一篇: AI理论知识基础(25)-机器学习常见损