日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Vector源码

發布時間:2024/10/14 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Vector源码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.同步

它的實現與ArrayList 類似,但是使用了synchronized 進行同步。

public synchronized boolean add(E e) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = e;return true; } public synchronized E get(int index) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);return elementData(index); }

2.?與 ArrayList 的比較

Vector 是同步的,因此開銷就比 ArrayList 要大,訪問速度更慢。

最好使用ArrayList 而不是 Vector,因為同步操作完全可以由程序員自己來控制;

Vector 每次擴容請求其大小的 2 倍空間,而 ArrayList 是 1.5 倍。

3. 替代方案

可以使用 Collections.synchronizedList(); 得到一個線程安全的 ArrayList。

List<String> list = new ArrayList<>(); List<String> synList = Collections.synchronizedList(list);

也可以使用 concurrent 并發包下的 CopyOnWriteArrayList 類。

List<String> list = new CopyOnWriteArrayList<>();

?

總結

以上是生活随笔為你收集整理的Vector源码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。