Java源碼閱讀Vector

來源:互聯網
上載者:User

標籤:dep   effort   訪問   modified   member   object   sse   odi   cap   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.