1. 程式人生 > >Java源碼閱讀Vector

Java源碼閱讀Vector

lin ever best com 機制 imp elements integer ray

1類註釋

/**
 * The {@code Vector} class implements a growable array of
 * objects. Like an array, it contains components that can be
 * accessed using an integer index. However, the size of a
 * {@code Vector} can grow or shrink as needed to accommodate
 * adding and removing items after the {@code
Vector} has been created. * * <p>Each vector tries to optimize storage management by maintaining a * {@code capacity} and a {@code capacityIncrement}. The * {@code capacity} is always at least as large as the vector * size; it is usually larger because as components are added to the * vector, the vector‘s storage increases in chunks the size of * {
@code capacityIncrement}. An application can increase the * capacity of a vector before inserting a large number of * components; this reduces the amount of incremental reallocation. * * <p><a name="fail-fast"> * The iterators returned by this class‘s {@link #iterator() iterator} and * {
@link #listIterator(int) listIterator} methods are <em>fail-fast</em></a>: * if the vector is structurally modified at any time after the iterator is * created, in any way except through the iterator‘s own * {@link ListIterator#remove() remove} or * {@link ListIterator#add(Object) add} methods, the iterator will throw a * {@link ConcurrentModificationException}. Thus, in the face of * concurrent modification, the iterator fails quickly and cleanly, rather * than risking arbitrary, non-deterministic behavior at an undetermined * time in the future. The {@link Enumeration Enumerations} returned by * the {@link #elements() elements} method are <em>not</em> fail-fast. * * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed * as it is, generally speaking, impossible to make any hard guarantees in the * presence of unsynchronized concurrent modification. Fail-fast iterators * throw {@code ConcurrentModificationException} on a best-effort basis. * Therefore, it would be wrong to write a program that depended on this * exception for its correctness: <i>the fail-fast behavior of iterators * should be used only to detect bugs.</i> * * <p>As of the Java 2 platform v1.2, this class was retrofitted to * implement the {@link List} interface, making it a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. Unlike the new collection * implementations, {@code Vector} is synchronized. If a thread-safe * implementation is not needed, it is recommended to use {@link * ArrayList} in place of {@code Vector}.
*/

Vector類實現了一個可伸縮的對象數組。和數組一樣,他裏面的組件能被integer型的索引訪問。不同的是,Vector被創建後,當增加或刪除項的時候,其size可以增長或收縮來適應大小。

每個Vector都試圖通過保持capacity和capacityIncrement來優化存儲管理。 capacity總是至少和vector的size一樣大,它通常是更大的因為組件添加到Vector,Vector的存儲塊的大小增加“capacityincrement”。應用程序可以在插入大量組件之前增加Vector的capacity,這減少了增量再分配的數量。

通過該類的iterator()和listIterator(int)方法返回的iterator是fail-fast(快速失敗機制)的:

當iterator被創建後,如果vector的結構在任何時候被修改,除了使用iterator自己的ListIterator#remove()或者ListIterator#add(Object)外的任何方法,將會拋出ConcurrentModificationException異常。因此,面對並發修改時,叠代器會快速而幹凈地失敗,而不是在未來的某個不確定的時間進行有風險的行為。被elements()方法返回的Enumeration不是fail-fast的。

值得註意的是fail-fast行為可能是不能得到保證的。因此,編寫一個依賴於ConcurrentModificationException異常的程序是錯誤的:叠代器的fail-fast行為應該只用於檢測錯誤。

在Java 2版本中,這個類被修改為實現List接口,使得它成為java集合框架成員。Vector是synchronized的(線程安全的),如果不需要線程安全的實現,推薦使用ArrayList。(效率更高)

2

Java源碼閱讀Vector