源码分析——What is LinkedList

Posted 喵喵7781

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码分析——What is LinkedList相关的知识,希望对你有一定的参考价值。

Doubly-linked list implementation of the @code List and @code Dequeinterfaces.  Implements all optional list operations, and permits all elements (including @code null).

双向链表实现了List接口和Deque接口。实现所有list的操作,并允许所有元素(包括@code null)。


 <p>All of the operations perform as could be expected for a doubly-linked list.  Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

 对于双向链表,所有操作都可以预期。索引到列表中的操作将从开头或结尾遍历列表,以较接近指定索引为准。


 <p><strong>Note that this implementation is not synchronized.</strong> If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it <i>must</i> be synchronized externally.  (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.)  This is typically accomplished by synchronizing on some object that naturally encapsulates the list.

请注意,此实现未同步。如果多个线程同时访问链接列表,并且至少有一个线程在结构上修改了列表,则必须同步外部。 (结构修改是添加或删除一个或多个元素的任何操作;仅设置元素的值不是结构修改。)这通常通过同步自然封装列表的某个对象来完成。


If no such object exists, the list should be "wrapped" using the @link Collections#synchronizedList ollections.synchronizedList
 method.  This is best done at creation time, to prevent accidental unsynchronized access to the list:<pre>   List list = Collections.synchronizedList(new LinkedList(...));</pre>


 如果不存在此类对象,则应使用@link CollectionssynchronizedList ollections.synchronizedList“包装”该列表。
方法。这最好在创建时完成,以防止意外地不同步访问列表:<pre> List list = Collections.synchronizedList(new LinkedList(...)); </ pre>


 <p>The iterators returned by this class's @code iterator and @code listIterator methods are <i>fail-fast</i>: if the list is
 structurally modified at any time after the iterator is created, in any way except through the Iterator's own @code remove or
 @code 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.

此类的@code iterator和@code listIterator方法返回的迭代器是 fail-fast :如果列表是在创建迭代器之后的任何时候进行结构修改,除了通过Iterator自己的@code remove或者@code add方法,迭代器将抛出@link ConcurrentModificationException。因此,面对并发修改,迭代器快速而干净地失败,而不是冒着未确定的任意,非确定性行为的风险in the future。


  <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>

请注意,迭代器的 fail-fast行为无法得到保证,因为一般来说,在存在非同步并发修改的情况下,不可能做出任何硬性保证。失败快速的迭代器会尽最大努力抛出@code ConcurrentModificationException。因此,编写一个依赖于此异常的程序来保证正确性是错误的:迭代器的fail-fast 行为应仅用于检测错误。
 

以上是关于源码分析——What is LinkedList的主要内容,如果未能解决你的问题,请参考以下文章

源码分析——What is ConcurrentHashMap

源码分析——What is ConcurrentHashMap

源码分析——What is LinkedHashMap

源码分析——What is LinkedHashMap

源码分析——What is ArrayList

源码分析——What is ArrayList