LinkedList源码

来源:互联网 发布:猪肉绦虫知乎 编辑:程序博客网 时间:2024/06/01 07:40

1、非常巧妙的部分的部分:

  • 把所有的检查边界抽象成一个方法了:checkPositionIndex
  • 查找某节点 使用了一次比较,使用了一次折半查找

2、 难点

  • LLSpliterator 用于实现并行的迭代器

3、 遗留问题

  • java8 新特性
//lambpublic void forEachRemaining(Consumer<? super E> action) {            Objects.requireNonNull(action);            while (modCount == expectedModCount && nextIndex < size) {                action.accept(next.item);                lastReturned = next;                next = next.next;                nextIndex++;            }            checkForComodification();        }// 读写问题    private void defaultWriteFields(Object obj, ObjectStreamClass desc)        throws IOException    {        Class<?> cl = desc.forClass();        if (cl != null && obj != null && !cl.isInstance(obj)) {            throw new ClassCastException();        }        desc.checkDefaultSerialize();        int primDataSize = desc.getPrimDataSize();        if (primVals == null || primVals.length < primDataSize) {            primVals = new byte[primDataSize];        }        desc.getPrimFieldValues(obj, primVals);        bout.write(primVals, 0, primDataSize, false);        ObjectStreamField[] fields = desc.getFields(false);        Object[] objVals = new Object[desc.getNumObjFields()];        int numPrimFields = fields.length - objVals.length;        desc.getObjFieldValues(obj, objVals);        for (int i = 0; i < objVals.length; i++) {            if (extendedDebugInfo) {                debugInfoStack.push(                    "field (class \"" + desc.getName() + "\", name: \"" +                    fields[numPrimFields + i].getName() + "\", type: \"" +                    fields[numPrimFields + i].getType() + "\")");            }            try {                writeObject0(objVals[i],                             fields[numPrimFields + i].isUnshared());            } finally {                if (extendedDebugInfo) {                    debugInfoStack.pop();                }            }        }    }//实现并发的 iterator    /** A customized variant of Spliterators.IteratorSpliterator */    static final class LLSpliterator<E> implements Spliterator<E> {        static final int BATCH_UNIT = 1 << 10;  // batch array size increment        static final int MAX_BATCH = 1 << 25;  // max batch array size;        final LinkedList<E> list; // null OK unless traversed        Node<E> current;      // current node; null until initialized        int est;              // size estimate; -1 until first needed        int expectedModCount; // initialized when est set        int batch;            // batch size for splits        LLSpliterator(LinkedList<E> list, int est, int expectedModCount) {            this.list = list;            this.est = est;            this.expectedModCount = expectedModCount;        }        final int getEst() {            int s; // force initialization            final LinkedList<E> lst;            if ((s = est) < 0) {                if ((lst = list) == null)                    s = est = 0;                else {                    expectedModCount = lst.modCount;                    current = lst.first;                    s = est = lst.size;                }            }            return s;        }        public long estimateSize() { return (long) getEst(); }        public Spliterator<E> trySplit() {            Node<E> p;            int s = getEst();            if (s > 1 && (p = current) != null) {                int n = batch + BATCH_UNIT;                if (n > s)                    n = s;                if (n > MAX_BATCH)                    n = MAX_BATCH;                Object[] a = new Object[n];                int j = 0;                do { a[j++] = p.item; } while ((p = p.next) != null && j < n);                current = p;                batch = j;                est = s - j;                return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);            }            return null;        }        public void forEachRemaining(Consumer<? super E> action) {            Node<E> p; int n;            if (action == null) throw new NullPointerException();            if ((n = getEst()) > 0 && (p = current) != null) {                current = null;                est = 0;                do {                    E e = p.item;                    p = p.next;                    action.accept(e);                } while (p != null && --n > 0);            }            if (list.modCount != expectedModCount)                throw new ConcurrentModificationException();        }        public boolean tryAdvance(Consumer<? super E> action) {            Node<E> p;            if (action == null) throw new NullPointerException();            if (getEst() > 0 && (p = current) != null) {                --est;                E e = p.item;                current = p.next;                action.accept(e);                if (list.modCount != expectedModCount)                    throw new ConcurrentModificationException();                return true;            }            return false;        }        public int characteristics() {            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;        }    }