日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Collections.synchronizedList使用

發(fā)布時(shí)間:2025/3/12 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Collections.synchronizedList使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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ū)別:

  • Vector擴(kuò)容為原來(lái)的2倍長(zhǎng)度,ArrayList擴(kuò)容為原來(lái)1.5倍
  • SynchronizedList有很好的擴(kuò)展和兼容功能。他可以將所有的List的子類轉(zhuǎn)成線程安全的類。
  • 使用SynchronizedList的時(shí)候,進(jìn)行遍歷時(shí)要手動(dòng)進(jìn)行同步處理 。
  • SynchronizedList可以指定鎖定的對(duì)象。
  • 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)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。