From dc1bf93cab6b90c2bb90a6e0e209e77711d47608 Mon Sep 17 00:00:00 2001 From: puhaiyang Date: Mon, 9 Mar 2020 11:22:15 +0800 Subject: [PATCH 1/9] =?UTF-8?q?070=5Fweek01=5F=E4=BD=9C=E4=B8=9A=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_01/70/ArrayList-070.adoc | 255 ++++++++++++++++++++++++++ second/week_01/70/HashMap-070.adoc | 106 +++++++++++ second/week_01/70/LinkedList-070.adoc | 242 ++++++++++++++++++++++++ 3 files changed, 603 insertions(+) create mode 100644 second/week_01/70/ArrayList-070.adoc create mode 100644 second/week_01/70/HashMap-070.adoc create mode 100644 second/week_01/70/LinkedList-070.adoc diff --git a/second/week_01/70/ArrayList-070.adoc b/second/week_01/70/ArrayList-070.adoc new file mode 100644 index 0000000..c05780c --- /dev/null +++ b/second/week_01/70/ArrayList-070.adoc @@ -0,0 +1,255 @@ += ArrayList基础入门学习笔记 +Doc Writer +v1.0, 2020-03-08 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- arraylist中默认大小是多少? +- arraylist何时扩容?每次扩容是怎么扩容的,过程是怎么样的 +- arraylist线程不安全的体现在哪里?如何解决 +- arraylist遍历时进行删除时会报异常,用迭代器就不会抛出异常的原理是什么? +- arraylist中add的过程 + +== 部分源码分析笔记 +=== arraylist无参构造方法 + +[source,adoc] +---- + /** + * Constructs an empty list with an initial capacity of ten. + */ + public ArrayList() { + this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; + } +---- + +capacity of ten体现点?查看add方法 + +=== add方法 +[source,adoc] +---- + /** + * Appends the specified element to the end of this list. + * + * @param e element to be appended to this list + * @return true (as specified by {@link Collection#add}) + */ + public boolean add(E e) { + //size为int类型,默认为0 + ensureCapacityInternal(size + 1); // Increments modCount!! + elementData[size++] = e; + return true; + } + + // DEFAULTCAPACITY_EMPTY_ELEMENTDATA为空数组 + private void ensureCapacityInternal(int minCapacity) { + //最小容量大小为10 + if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + ensureExplicitCapacity(minCapacity); + } + + private void ensureExplicitCapacity(int minCapacity) { + //modCount++,modCount默认为0,每次调用add方法时都会增加一次 + modCount++; + + // overflow-conscious code + if (minCapacity - elementData.length > 0) + grow(minCapacity); + //当minCapacity比elementData.length大时,就会执行grow操作 + + } + + + /** + * Increases the capacity to ensure that it can hold at least the + * number of elements specified by the minimum capacity argument. + * + * @param minCapacity the desired minimum capacity + */ + private void grow(int minCapacity) { + // overflow-conscious code + int oldCapacity = elementData.length; + //在旧容量的基础上右移一位,如1010变为了101,即增加原始大小的百分之50 + int newCapacity = oldCapacity + (oldCapacity >> 1); + //??????这个操作有啥用? + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + // MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;hugeCapacity确保不会让arrayList越界,同时也让证明了arraylist并不是可以无限大的 + if (newCapacity - MAX_ARRAY_SIZE > 0) + newCapacity = hugeCapacity(minCapacity); + //将原始数姐进行拷贝,底曾调用的是System.arraycopy,这是一个navite方法 + // minCapacity is usually close to size, so this is a win: + elementData = Arrays.copyOf(elementData, newCapacity); + } +---- + +[NOTE] +.通过add方法可知 +==== +查看源码后可知如下内容: + +. arrayList在没有指定大小的情况下,size=0 +. arrayList的扩容不会提前先扩容,而是在原始大小装不下时才会进行扩容 +. arrayList每次扩容时的大小为原始大小是1半,用的位移运算来操作的,每次向右移动了一位 +. arrayList的最大长度为Integer.MAX_VALUE,说明arrayList并不是可以无限增长的,它也是有极限的 +. arrayList的扩容过程用的是system.arraycopy来实现的 +==== + +=== remove方法 +[source,adoc] +---- + /** + * Removes the first occurrence of the specified element from this list, + * if it is present. If the list does not contain the element, it is + * unchanged. More formally, removes the element with the lowest index + * i such that + * (o==null ? get(i)==null : o.equals(get(i))) + * (if such an element exists). Returns true if this list + * contained the specified element (or equivalently, if this list + * changed as a result of the call). + * + * @param o element to be removed from this list, if present + * @return true if this list contained the specified element + */ + public boolean remove(Object o) { + //遍历判断值是否相等,找到那个相等的值,如果找到了则调用fastRemove方法进行移除 + if (o == null) { + for (int index = 0; index < size; index++) + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } else { + for (int index = 0; index < size; index++) + if (o.equals(elementData[index])) { + fastRemove(index); + return true; + } + } + return false; + } + + /* + * Private remove method that skips bounds checking and does not + * return the value removed. + */ + private void fastRemove(int index) { + //进行移除时,modCount也会增加 + modCount++; + //0 1 2 3 4 5,共6个,想要移除第2个,则需要变动的元素个数为6-2-1=3 + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, + numMoved); + elementData[--size] = null; // clear to let GC do its work + } + + /** arraycopy中的5个参数 + * @param src the source array. + * @param srcPos starting position in the source array. + * @param dest the destination array. + * @param destPos starting position in the destination data. + * @param length the number of array elements to be copied. + **/ +---- +[NOTE] +.通过remove方法可知 +==== +查看源码后可知如下内容: + +. remove方法是也是通过system.arraycopy来实现的 +==== + +=== iterator方法 +[source,java] +---- + /** + * An optimized version of AbstractList.Itr + */ + private class Itr implements Iterator { + int cursor; // index of next element to return + int lastRet = -1; // index of last element returned; -1 if no such + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + @SuppressWarnings("unchecked") + public E next() { + checkForComodification(); + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return (E) elementData[lastRet = i]; + } + + //迭代器中的remove实际上调用的还是ArrayList中的remove,通过索引来remove元素的 + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + @SuppressWarnings("unchecked") + public void forEachRemaining(Consumer consumer) { + Objects.requireNonNull(consumer); + final int size = ArrayList.this.size; + int i = cursor; + if (i >= size) { + return; + } + final Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) { + throw new ConcurrentModificationException(); + } + while (i != size && modCount == expectedModCount) { + consumer.accept((E) elementData[i++]); + } + // update once at end of iteration to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); + } + + //确保在遍历期间没有对arraylist进行修改 + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } +---- + +[NOTE] +.通过iterator方法可知 +==== +查看源码后可知如下内容: + +. arrayList中的iterator里的remove方法调用的本质其实也是调用的arraylist的remove索引来进行删除的 + +==== diff --git a/second/week_01/70/HashMap-070.adoc b/second/week_01/70/HashMap-070.adoc new file mode 100644 index 0000000..6586e1d --- /dev/null +++ b/second/week_01/70/HashMap-070.adoc @@ -0,0 +1,106 @@ += Hashmap基础入门学习笔记 +Doc Writer +v1.0, 2020-03-08 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- hashmap中的结构何时会变为红黑树 +- hashmap线程不安全的体现点在哪里?jdk.7与jdk1.8的不同点(对于jdk1.7中hashmap线程不安全可能会导致死锁的原因这里不再重新记录源码,详见本人的历史博文 +https://blog.csdn.net/puhaiyang/article/details/90812572[hashmap从入门到死锁,再到分段式锁] +- hashmap在jdk1.7中 + +== 部分源码分析笔记 + +=== hashmap的put方法 +[source,adoc] +---- + /** + * Associates the specified value with the specified key in this map. + * If the map previously contained a mapping for the key, the old + * value is replaced. + * + * @param key key with which the specified value is to be associated + * @param value value to be associated with the specified key + * @return the previous value associated with key, or + * null if there was no mapping for key. + * (A null return can also indicate that the map + * previously associated null with key.) + */ + public V put(K key, V value) { + return putVal(hash(key), key, value, false, true); + } + + /** + * Implements Map.put and related methods. + * + * @param hash hash for key + * @param key the key + * @param value the value to put + * @param onlyIfAbsent if true, don't change existing value + * @param evict if false, the table is in creation mode. + * @return previous value, or null if none + */ + final V putVal(int hash, K key, V value, boolean onlyIfAbsent, + boolean evict) { + Node[] tab; Node p; int n, i; + //判断hashmap是否需要进行初始化 + if ((tab = table) == null || (n = tab.length) == 0) + n = (tab = resize()).length; + //判断value是否为null值 + if ((p = tab[i = (n - 1) & hash]) == null) + tab[i] = newNode(hash, key, value, null); + else { + Node e; K k; + if (p.hash == hash && + ((k = p.key) == key || (key != null && key.equals(k)))) + e = p; + else if (p instanceof TreeNode) + e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value); + else { + for (int binCount = 0; ; ++binCount) { + if ((e = p.next) == null) { + p.next = newNode(hash, key, value, null); + //判断是否需要将hashmap节点转为红黑树,TREEIFY_THRESHOLD为8 + if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st + //java.util.HashMap.TreeNode为主要算法代码 + treeifyBin(tab, hash); + break; + } + if (e.hash == hash && + ((k = e.key) == key || (key != null && key.equals(k)))) + break; + p = e; + } + } + if (e != null) { // existing mapping for key + V oldValue = e.value; + if (!onlyIfAbsent || oldValue == null) + e.value = value; + afterNodeAccess(e); + return oldValue; + } + } + ++modCount; + if (++size > threshold) + resize(); + // Callbacks to allow LinkedHashMap post-actions,此方法为一个回调方法 + afterNodeInsertion(evict); + return null; + } +---- + +[NOTE] +.通过put方法可知 +==== +查看源码后可知如下内容: + +. hashmap在jdk1.8中,如果key节点的value数大于等于8时,会将此节点转为一个红黑树,以加快查找时间 +==== diff --git a/second/week_01/70/LinkedList-070.adoc b/second/week_01/70/LinkedList-070.adoc new file mode 100644 index 0000000..66932fe --- /dev/null +++ b/second/week_01/70/LinkedList-070.adoc @@ -0,0 +1,242 @@ += LinkedList基础入门学习笔记 +Doc Writer +v1.0, 2020-03-08 +:toc-title: 目录 +:toc: left + +== 基本要求 + +=== 源码学习要求 + +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- 与linkedList的区别?存储结构 + + +== 部分源码分析笔记 +=== LinkedList的add方法 +[source,adoc] +---- + /** + * Constructs an empty list. + */ + public LinkedList() { + } + + /** + * Appends the specified element to the end of this list. + * + *

This method is equivalent to {@link #addLast}. + * + * @param e element to be appended to this list + * @return {@code true} (as specified by {@link Collection#add}) + */ + public boolean add(E e) { + linkLast(e); + return true; + } + + /** + * Links e as last element. + */ + void linkLast(E e) { + //last默认为null + final Node l = last; + //构造节点 + final Node newNode = new Node<>(l, e, null); + last = newNode; + //如果last节点是空的,就让当前节点作为最后一个节点,链表只有一个元素 + if (l == null) + first = newNode; + else + l.next = newNode; + size++; + modCount++; + } + +---- + +[NOTE] +.通过put方法可知 +==== +查看源码后可知如下内容: + +. linkedlist由于是基于链表操作的,其存储结构便是逻辑上的关联关系,从代码上没有看到长度大小限制 +==== + +=== LinkedList的iterator方法 +[source,adoc] +---- + // Iterators + + /** + * Returns an iterator over the elements in this list (in proper + * sequence).

+ * + * This implementation merely returns a list iterator over the list. + * + * @return an iterator over the elements in this list (in proper sequence) + */ + public Iterator iterator() { + return listIterator(); + } + + /** + * {@inheritDoc} + * + *

This implementation returns {@code listIterator(0)}. + * + * @see #listIterator(int) + */ + public ListIterator listIterator() { + return listIterator(0); + } + + /** + * Returns a list-iterator of the elements in this list (in proper + * sequence), starting at the specified position in the list. + * Obeys the general contract of {@code List.listIterator(int)}.

+ * + * The list-iterator is fail-fast: if the list is structurally + * modified at any time after the Iterator is created, in any way except + * through the list-iterator's own {@code remove} or {@code add} + * methods, the list-iterator will throw a + * {@code 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. + * + * @param index index of the first element to be returned from the + * list-iterator (by a call to {@code next}) + * @return a ListIterator of the elements in this list (in proper + * sequence), starting at the specified position in the list + * @throws IndexOutOfBoundsException {@inheritDoc} + * @see List#listIterator(int) + */ + public ListIterator listIterator(int index) { + checkPositionIndex(index); + return new ListItr(index); + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } +---- + +其ListItr源码为: +[source,java] +---- +private class ListItr implements ListIterator { +private Node lastReturned; +private Node next; +private int nextIndex; +private int expectedModCount = modCount; + + ListItr(int index) { + // assert isPositionIndex(index); + next = (index == size) ? null : node(index); + nextIndex = index; + } + + public boolean hasNext() { + return nextIndex < size; + } + + public E next() { + checkForComodification(); + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.item; + } + + public boolean hasPrevious() { + return nextIndex > 0; + } + + public E previous() { + checkForComodification(); + if (!hasPrevious()) + throw new NoSuchElementException(); + + lastReturned = next = (next == null) ? last : next.prev; + nextIndex--; + return lastReturned.item; + } + + public int nextIndex() { + return nextIndex; + } + + public int previousIndex() { + return nextIndex - 1; + } + + public void remove() { + checkForComodification(); + if (lastReturned == null) + throw new IllegalStateException(); + + Node lastNext = lastReturned.next; + unlink(lastReturned); + if (next == lastReturned) + next = lastNext; + else + nextIndex--; + lastReturned = null; + expectedModCount++; + } + + public void set(E e) { + if (lastReturned == null) + throw new IllegalStateException(); + checkForComodification(); + lastReturned.item = e; + } + + public void add(E e) { + checkForComodification(); + lastReturned = null; + if (next == null) + linkLast(e); + else + linkBefore(e, next); + nextIndex++; + expectedModCount++; + } + + public void forEachRemaining(Consumer action) { + Objects.requireNonNull(action); + while (modCount == expectedModCount && nextIndex < size) { + action.accept(next.item); + lastReturned = next; + next = next.next; + nextIndex++; + } + checkForComodification(); + } + + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } +---- + +[NOTE] +.通过iterator方法可知 +==== +查看源码后可知如下内容: + +. iterator的遍历实际上就是链表的遍历,而arraylist中的遍历是索引的遍历 +. 也是通过迭代器中可以看出linkedlist的数据删除是让节点的前后节点直接关联即可 +. 正是因为其结构的特性,linkedlist与arrayList相比,一般常认为arraylist对于数据随机访问比较快,因为linkedlist需要一个一个节点的遍历并计数才能获取出最终的结果.而arraylist的删除操作也是同样的,它的删除一般需要将数组的节点进行复制,时间与空间复杂点消耗较大 +==== \ No newline at end of file -- Gitee From 1183fcb7a52d891217a9b80d5cedfed4f07fcd17 Mon Sep 17 00:00:00 2001 From: puhaiyang Date: Mon, 9 Mar 2020 12:28:10 +0800 Subject: [PATCH 2/9] =?UTF-8?q?1.=E5=88=9D=E5=A7=8B=E5=8C=96=E6=9C=AC?= =?UTF-8?q?=E5=91=A8=E8=A6=81=E5=AD=A6=E4=B9=A0=E7=9A=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/AtomicInteger-070.adoc | 0 second/week_02/70/AtomicStampedReference-070.adoc | 0 second/week_02/70/LongAdder-070.adoc | 0 second/week_02/70/Striped64-070.adoc | 0 second/week_02/70/Unsafe-070.adoc | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 second/week_02/70/AtomicInteger-070.adoc create mode 100644 second/week_02/70/AtomicStampedReference-070.adoc create mode 100644 second/week_02/70/LongAdder-070.adoc create mode 100644 second/week_02/70/Striped64-070.adoc create mode 100644 second/week_02/70/Unsafe-070.adoc diff --git a/second/week_02/70/AtomicInteger-070.adoc b/second/week_02/70/AtomicInteger-070.adoc new file mode 100644 index 0000000..e69de29 diff --git a/second/week_02/70/AtomicStampedReference-070.adoc b/second/week_02/70/AtomicStampedReference-070.adoc new file mode 100644 index 0000000..e69de29 diff --git a/second/week_02/70/LongAdder-070.adoc b/second/week_02/70/LongAdder-070.adoc new file mode 100644 index 0000000..e69de29 diff --git a/second/week_02/70/Striped64-070.adoc b/second/week_02/70/Striped64-070.adoc new file mode 100644 index 0000000..e69de29 diff --git a/second/week_02/70/Unsafe-070.adoc b/second/week_02/70/Unsafe-070.adoc new file mode 100644 index 0000000..e69de29 -- Gitee From ac0de7e6f1ba53a7e569cfb7a39c547926802e7e Mon Sep 17 00:00:00 2001 From: puhaiyang Date: Mon, 9 Mar 2020 13:53:15 +0800 Subject: [PATCH 3/9] =?UTF-8?q?1.=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/Unsafe-070.adoc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/second/week_02/70/Unsafe-070.adoc b/second/week_02/70/Unsafe-070.adoc index e69de29..44cb71d 100644 --- a/second/week_02/70/Unsafe-070.adoc +++ b/second/week_02/70/Unsafe-070.adoc @@ -0,0 +1,26 @@ += Unsage基础入门学习笔记 +Doc Writer +v1.0, 2020-03-09 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- Unsafe有什么用? +- Unsafe如何调用? + + +== 部分源码分析笔记 +=== Unsafe如何获得 +[source,adoc] +---- + Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); +---- \ No newline at end of file -- Gitee From 9ca8be8d1edd1528ae4a71ed4a3b43ad365cc276 Mon Sep 17 00:00:00 2001 From: puhaiyang Date: Fri, 13 Mar 2020 17:44:27 +0800 Subject: [PATCH 4/9] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0Unsafe=E7=9A=84?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/Unsafe-070.adoc | 87 ++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/second/week_02/70/Unsafe-070.adoc b/second/week_02/70/Unsafe-070.adoc index 44cb71d..ebad153 100644 --- a/second/week_02/70/Unsafe-070.adoc +++ b/second/week_02/70/Unsafe-070.adoc @@ -17,10 +17,95 @@ jdk1.8 == 部分源码分析笔记 +=== Unsafe的getUnsafe方法 +[source,adoc] +---- + @CallerSensitive + public static Unsafe getUnsafe() { + Class var0 = Reflection.getCallerClass(); + //如果classLoader不为空,则会抛出异常 + if (!VM.isSystemDomainLoader(var0.getClassLoader())) { + throw new SecurityException("Unsafe"); + } else { + return theUnsafe; + } + } + + @CallerSensitive + public static native Class getCallerClass(); + + /** + * Returns the class loader for the class. Some implementations may use + * null to represent the bootstrap class loader. This method will return + * null in such implementations if this class was loaded by the bootstrap + * class loader. + * + *

If a security manager is present, and the caller's class loader is + * not null and the caller's class loader is not the same as or an ancestor of + * the class loader for the class whose class loader is requested, then + * this method calls the security manager's {@code checkPermission} + * method with a {@code RuntimePermission("getClassLoader")} + * permission to ensure it's ok to access the class loader for the class. + * + *

If this object + * represents a primitive type or void, null is returned. + * + * @return the class loader that loaded the class or interface + * represented by this object. + * @throws SecurityException + * if a security manager exists and its + * {@code checkPermission} method denies + * access to the class loader for the class. + * @see java.lang.ClassLoader + * @see SecurityManager#checkPermission + * @see java.lang.RuntimePermission + */ + @CallerSensitive + public ClassLoader getClassLoader() { + ClassLoader cl = getClassLoader0(); + if (cl == null) + return null; + SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass()); + } + return cl; + } + + public static boolean isSystemDomainLoader(ClassLoader var0) { + return var0 == null; + } +---- +[NOTE] +.通过getUnsafe代码中可以看出 +==== +查看源码后可知如下内容: +. 直接调用会抛出安全异常,因为如果不是由JVM调用的,那么classloader就不会为空 +. CallerSensitive注解,是出于安全设计的,它会直接找到调用者 + +==== + === Unsafe如何获得 +通过反射进入实现 [source,adoc] ---- Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); Unsafe unsafe = (Unsafe) theUnsafe.get(null); ----- \ No newline at end of file +---- + +=== Unsafe的常见方法 +[source,adoc] +---- + public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5); + + public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5); + + public final native boolean compareAndSwapLong(Object var1, long var2, long var4, long var6); + + public native void unpark(Object var1); + + public native void park(boolean var1, long var2); +---- + +NOTE: 全是navive方法 -- Gitee From c572c0f02abef3c33a2afa509da7fda569fcbdf9 Mon Sep 17 00:00:00 2001 From: puhaiyang Date: Fri, 13 Mar 2020 17:46:29 +0800 Subject: [PATCH 5/9] =?UTF-8?q?1.=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/AtomicInteger-070.adoc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/second/week_02/70/AtomicInteger-070.adoc b/second/week_02/70/AtomicInteger-070.adoc index e69de29..8b040fd 100644 --- a/second/week_02/70/AtomicInteger-070.adoc +++ b/second/week_02/70/AtomicInteger-070.adoc @@ -0,0 +1,21 @@ += AtomicInteger基础入门学习笔记 +Doc Writer +v1.0, 2020-03-13 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- AtomicInteger有什么用? +- AtomicInteger是如何实现原子性的? + + +== 部分源码分析笔记 +=== AtomicInteger构造方法 + -- Gitee From c620e7020771d1abb672ba0b1fc60f0d2b2e31b4 Mon Sep 17 00:00:00 2001 From: puhaiyang <761396462@qq.com> Date: Sat, 14 Mar 2020 16:21:55 +0800 Subject: [PATCH 6/9] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0AtomicInteger=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/AtomicInteger-070.adoc | 71 +++++++++++++++++++++++- second/week_02/70/Unsafe-070.adoc | 24 ++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/second/week_02/70/AtomicInteger-070.adoc b/second/week_02/70/AtomicInteger-070.adoc index 8b040fd..a401c40 100644 --- a/second/week_02/70/AtomicInteger-070.adoc +++ b/second/week_02/70/AtomicInteger-070.adoc @@ -12,10 +12,77 @@ v1.0, 2020-03-13 jdk1.8 === 学习后需要至少掌握的知识点 -- AtomicInteger有什么用? +- AtomicInteger与Integer有什么区别? - AtomicInteger是如何实现原子性的? == 部分源码分析笔记 -=== AtomicInteger构造方法 +=== AtomicInteger类与Integer类 +Integer +[source,adoc] +---- +public final class Integer extends Number implements Comparable +---- + +AtomicInteger +[source,adoc] +---- +public class AtomicInteger extends Number implements java.io.Serializable +---- + +通过类的定义可知道Integer与AtomicInteger的特点 +|=== +|类名 |相同点 |不同点 + +|Integer,AtomicInteger +|都继承于Number +|Integer为Final类,实现了Comparable接口 + +|=== + +=== AtomicInteger源码部分学习笔记 +[source,java] +---- +public class AtomicInteger extends Number implements java.io.Serializable { + private static final long serialVersionUID = 6214790243416807050L; + + // setup to use Unsafe.compareAndSwapInt for updates + private static final Unsafe unsafe = Unsafe.getUnsafe(); + //初始值为0,静态代码块会将此值进行修改 + private static final long valueOffset; + + static { + try { + //获取value值在对象中的内存地址,指针地址,为啥要获取?因为oop-class模型设计如此吧 + valueOffset = unsafe.objectFieldOffset + (AtomicInteger.class.getDeclaredField("value")); + } catch (Exception ex) { throw new Error(ex); } + } + + //使用volatile,确保多线程下的线程可见性 + private volatile int value; + + /** + * Creates a new AtomicInteger with the given initial value. + * + * @param initialValue the initial value + */ + public AtomicInteger(int initialValue) { + value = initialValue; + } + + /** + * Atomically increments by one the current value. + * + * @return the updated value + */ + public final int incrementAndGet() { + //即:将valueOffset这个内存地址的值加1,并返回原来的值 + return unsafe.getAndAddInt(this, valueOffset, 1) + 1; + } + +} +---- + + diff --git a/second/week_02/70/Unsafe-070.adoc b/second/week_02/70/Unsafe-070.adoc index ebad153..9643bff 100644 --- a/second/week_02/70/Unsafe-070.adoc +++ b/second/week_02/70/Unsafe-070.adoc @@ -99,6 +99,7 @@ jdk1.8 ---- public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5); + //cas实现赋值 public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5); public final native boolean compareAndSwapLong(Object var1, long var2, long var4, long var6); @@ -109,3 +110,26 @@ jdk1.8 ---- NOTE: 全是navive方法 + +=== Unsafe的getAndSetInt方法 +[source,adoc] +---- +/** + * var1代表要操作的对象 + * var2代表内存地址 + * var4代表要设置的值 + * 结合起来就是:要把var1这个对象中var2这个地址的值设为var4 + */ +public final int getAndSetInt(Object var1, long var2, int var4) { + int var5; + do { + //获取原始的值 + var5 = this.getIntVolatile(var1, var2); + //如果cas失败了,下面会返回false,然后它会重新获取var5的值,对其重新进行赋值; + //那么此getAndSetInt操作虽然是原子性的,但不是线程安全的 + } while(!this.compareAndSwapInt(var1, var2, var5, var4)); + + //返回旧值 + return var5; + } +---- -- Gitee From f62a819ed1bb91adda9c1a29c00b169582b2bbec Mon Sep 17 00:00:00 2001 From: puhaiyang <761396462@qq.com> Date: Sat, 14 Mar 2020 17:10:04 +0800 Subject: [PATCH 7/9] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0AtomicInteger=E4=B8=AD?= =?UTF-8?q?=E6=B5=8B=E8=AF=95ABA=E9=97=AE=E9=A2=98=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/AtomicInteger-070.adoc | 66 ++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/second/week_02/70/AtomicInteger-070.adoc b/second/week_02/70/AtomicInteger-070.adoc index a401c40..26ee3fc 100644 --- a/second/week_02/70/AtomicInteger-070.adoc +++ b/second/week_02/70/AtomicInteger-070.adoc @@ -86,3 +86,69 @@ public class AtomicInteger extends Number implements java.io.Serializable { +== ABA问题测试代码 +[source,adoc] +---- + private static void testForABA() throws Exception { + //没有感冒 + int notCaughtState = 666; + //感冒了 + int caughtState = 777; + + //小特的状态 + AtomicInteger atomicInteger = new AtomicInteger(notCaughtState); + + System.out.println("----很久以前,有个小孩叫【小特】"); + System.out.println("-----此刻:【小特】感冒了吗?-----" + ((atomicInteger.get() == notCaughtState) ? "没有感冒" : "感冒了")); + + + Thread cdcThread = new Thread(new Runnable() { + @Override + public void run() { + System.out.println("\n你好,我是" + Thread.currentThread().getName() + ",我的目标是治愈患者,现在开始工作"); + atomicInteger.compareAndSet(caughtState, notCaughtState); + System.out.println("我是" + Thread.currentThread().getName() + ",工作完毕"); + System.out.println("-----此刻:【小特】感冒了吗?-----" + ((atomicInteger.get() == notCaughtState) ? "没有感冒" : "感冒了")); + } + }, "cdc"); + + Thread virusTthread = new Thread(new Runnable() { + @Override + public void run() { + System.out.println("\n你好,我是" + Thread.currentThread().getName() + ",我的目标感染人体,现在开始工作"); + atomicInteger.compareAndSet(notCaughtState, caughtState); + System.out.println("我是" + Thread.currentThread().getName() + ",工作完毕"); + System.out.println("-----此刻:【小特】感冒了吗?-----" + ((atomicInteger.get() == notCaughtState) ? "没有感冒" : "感冒了")); + } + }, "virus"); + + virusTthread.start(); + + + cdcThread.start(); + + Thread.sleep(6); + int state = atomicInteger.get(); + if (state == notCaughtState) { + System.out.println("--------------------------------【ABA】【小特】没有感冒过,不需要进行隔离"); + } else { + System.out.println("--------------------------------【小特】感冒了"); + } + + } +---- +其输出结果有可能为: +[source,adoc] +---- +----很久以前,有个小孩叫【小特】 +-----此刻:【小特】感冒了吗?-----没有感冒 + +你好,我是virus,我的目标感染人体,现在开始工作 +我是virus,工作完毕 +-----此刻:【小特】感冒了吗?-----感冒了 + +你好,我是cdc,我的目标是治愈患者,现在开始工作 +我是cdc,工作完毕 +-----此刻:【小特】感冒了吗?-----没有感冒 +--------------------------------【ABA】【小特】没有感冒过,不需要进行隔离 +---- -- Gitee From d268e1e5f6203dde65ec36aa718ea459f2481e5c Mon Sep 17 00:00:00 2001 From: puhaiyang <761396462@qq.com> Date: Sat, 14 Mar 2020 22:56:40 +0800 Subject: [PATCH 8/9] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0AtomicStampedReference?= =?UTF-8?q?=E7=9A=84=E5=AD=A6=E4=B9=A0=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../70/AtomicStampedReference-070.adoc | 215 ++++++++++++++++++ second/week_02/70/Unsafe-070.adoc | 14 +- 2 files changed, 228 insertions(+), 1 deletion(-) diff --git a/second/week_02/70/AtomicStampedReference-070.adoc b/second/week_02/70/AtomicStampedReference-070.adoc index e69de29..705ddb1 100644 --- a/second/week_02/70/AtomicStampedReference-070.adoc +++ b/second/week_02/70/AtomicStampedReference-070.adoc @@ -0,0 +1,215 @@ += AtomicStampedReference基础入门学习笔记 +Doc Writer +v1.0, 2020-03-14 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- AtomicStampedReference是如何解决ABA问题的? + + +== 部分源码分析笔记 +[source,java] +---- +/** + * An {@code AtomicStampedReference} maintains an object reference + * along with an integer "stamp", that can be updated atomically. + * + *

Implementation note: This implementation maintains stamped + * references by creating internal objects representing "boxed" + * [reference, integer] pairs. + * + * @since 1.5 + * @author Doug Lea + * @param The type of object referred to by this reference + */ +public class AtomicStampedReference { + + private static class Pair { + //final修饰,则其值不能改变 + final T reference; + final int stamp; + private Pair(T reference, int stamp) { + this.reference = reference; + this.stamp = stamp; + } + static Pair of(T reference, int stamp) { + return new Pair(reference, stamp); + } + } + + private volatile Pair pair; + + /** + * Creates a new {@code AtomicStampedReference} with the given + * initial values. + * + * @param initialRef the initial reference + * @param initialStamp the initial stamp + */ + public AtomicStampedReference(V initialRef, int initialStamp) { + //初始化一个pair + pair = Pair.of(initialRef, initialStamp); + } + + /** + * Returns the current value of the reference. + * + * @return the current value of the reference + */ + public V getReference() { + return pair.reference; + } + + /** + * Returns the current value of the stamp. + * + * @return the current value of the stamp + */ + public int getStamp() { + return pair.stamp; + } + + /** + * Returns the current values of both the reference and the stamp. + * Typical usage is {@code int[1] holder; ref = v.get(holder); }. + * + * @param stampHolder an array of size of at least one. On return, + * {@code stampholder[0]} will hold the value of the stamp. + * @return the current value of the reference + */ + public V get(int[] stampHolder) { + Pair pair = this.pair; + stampHolder[0] = pair.stamp; + return pair.reference; + } + + /** + * Atomically sets the value of both the reference and stamp + * to the given update values if the + * current reference is {@code ==} to the expected reference + * and the current stamp is equal to the expected stamp. + * + *

May fail + * spuriously and does not provide ordering guarantees, so is + * only rarely an appropriate alternative to {@code compareAndSet}. + * + * @param expectedReference the expected value of the reference + * @param newReference the new value for the reference + * @param expectedStamp the expected value of the stamp + * @param newStamp the new value for the stamp + * @return {@code true} if successful + */ + public boolean weakCompareAndSet(V expectedReference, + V newReference, + int expectedStamp, + int newStamp) { + return compareAndSet(expectedReference, newReference, + expectedStamp, newStamp); + } + + /** + * Atomically sets the value of both the reference and stamp + * to the given update values if the + * current reference is {@code ==} to the expected reference + * and the current stamp is equal to the expected stamp. + * + * @param expectedReference the expected value of the reference + * @param newReference the new value for the reference + * @param expectedStamp the expected value of the stamp + * @param newStamp the new value for the stamp + * @return {@code true} if successful + */ + public boolean compareAndSet(V expectedReference, + V newReference, + int expectedStamp, + int newStamp) { + //获取当前pair + Pair current = pair; + return + //值相等 + expectedReference == current.reference && + //版本号相等 + expectedStamp == current.stamp && + ( + //版本号和值没有任何变动,则下面的代码也就不需要进行执行了 + (newReference == current.reference && newStamp == current.stamp) + || + //再调用Unsafe中的cas进行交换 + casPair(current, Pair.of(newReference, newStamp)) + ); + } + + /** + * Unconditionally sets the value of both the reference and stamp. + * + * @param newReference the new value for the reference + * @param newStamp the new value for the stamp + */ + public void set(V newReference, int newStamp) { + Pair current = pair; + if (newReference != current.reference || newStamp != current.stamp) + this.pair = Pair.of(newReference, newStamp); + } + + /** + * Atomically sets the value of the stamp to the given update value + * if the current reference is {@code ==} to the expected + * reference. Any given invocation of this operation may fail + * (return {@code false}) spuriously, but repeated invocation + * when the current value holds the expected value and no other + * thread is also attempting to set the value will eventually + * succeed. + * + * @param expectedReference the expected value of the reference + * @param newStamp the new value for the stamp + * @return {@code true} if successful + */ + public boolean attemptStamp(V expectedReference, int newStamp) { + Pair current = pair; + return + expectedReference == current.reference && + (newStamp == current.stamp || + casPair(current, Pair.of(expectedReference, newStamp))); + } + + // Unsafe mechanics + + private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe(); + //获取出pair在oop中的内存地址值 + private static final long pairOffset = + objectFieldOffset(UNSAFE, "pair", AtomicStampedReference.class); + + private boolean casPair(Pair cmp, Pair val) { + //这是一个native方法,也是通过cas进行交换 + return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val); + } + + static long objectFieldOffset(sun.misc.Unsafe UNSAFE, + String field, Class klazz) { + try { + return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); + } catch (NoSuchFieldException e) { + // Convert Exception to corresponding Error + NoSuchFieldError error = new NoSuchFieldError(field); + error.initCause(e); + throw error; + } + } +} +---- +[NOTE] +.通过查看AtomicStampedReference的代码中可以看出 +==== +查看源码后可知如下内容: +. AtomicStampedReference解决ABA问题是采用的添加stamp字段来解决的,即添加版本号 +. AtomicStampedReference中对于pair的交换也是用的Unsafe中的cas来进行处理的 + +==== \ No newline at end of file diff --git a/second/week_02/70/Unsafe-070.adoc b/second/week_02/70/Unsafe-070.adoc index 9643bff..5098685 100644 --- a/second/week_02/70/Unsafe-070.adoc +++ b/second/week_02/70/Unsafe-070.adoc @@ -5,19 +5,24 @@ v1.0, 2020-03-09 :toc: left == 基本要求 + === 源码学习要求 + - 至少提交2个类的源码分析笔记 === jdk环境要求 + jdk1.8 === 学习后需要至少掌握的知识点 + - Unsafe有什么用? - Unsafe如何调用? - == 部分源码分析笔记 + === Unsafe的getUnsafe方法 + [source,adoc] ---- @CallerSensitive @@ -76,6 +81,7 @@ jdk1.8 return var0 == null; } ---- + [NOTE] .通过getUnsafe代码中可以看出 ==== @@ -86,7 +92,9 @@ jdk1.8 ==== === Unsafe如何获得 + 通过反射进入实现 + [source,adoc] ---- Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); @@ -95,6 +103,7 @@ jdk1.8 ---- === Unsafe的常见方法 + [source,adoc] ---- public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5); @@ -112,6 +121,7 @@ jdk1.8 NOTE: 全是navive方法 === Unsafe的getAndSetInt方法 + [source,adoc] ---- /** @@ -133,3 +143,5 @@ public final int getAndSetInt(Object var1, long var2, int var4) { return var5; } ---- + +NOTE: 查看了数据cas部分的代码并通过自己的简单实践,感觉Unsafe中的许多方法便就是oop-class模型的入口方法, 通过此类的源码学习,对oop-class模型有了更进行一步的了解 -- Gitee From 9ddaf706446cd87dc0768d780fc5570325a60290 Mon Sep 17 00:00:00 2001 From: puhaiyang <761396462@qq.com> Date: Sun, 15 Mar 2020 22:08:03 +0800 Subject: [PATCH 9/9] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0LongAdder=E7=9A=84?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- second/week_02/70/LongAdder-070.adoc | 416 +++++++++++++++++++++++++++ second/week_02/70/Striped64-070.adoc | 400 ++++++++++++++++++++++++++ 2 files changed, 816 insertions(+) diff --git a/second/week_02/70/LongAdder-070.adoc b/second/week_02/70/LongAdder-070.adoc index e69de29..ddfc984 100644 --- a/second/week_02/70/LongAdder-070.adoc +++ b/second/week_02/70/LongAdder-070.adoc @@ -0,0 +1,416 @@ += LongAdder基础入门学习笔记 +Doc Writer +v1.0, 2020-03-15 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- LongAdder是干什么的,主要原理是什么? +- LongAdder与AtomicLong的区别? + + +== 部分源码分析笔记 +[source,java] +---- +/** + * One or more variables that together maintain an initially zero + * {@code long} sum. When updates (method {@link #add}) are contended + * across threads, the set of variables may grow dynamically to reduce + * contention. Method {@link #sum} (or, equivalently, {@link + * #longValue}) returns the current total combined across the + * variables maintaining the sum. + * + *

This class is usually preferable to {@link AtomicLong} when + * multiple threads update a common sum that is used for purposes such + * as collecting statistics, not for fine-grained synchronization + * control. Under low update contention, the two classes have similar + * characteristics. But under high contention, expected throughput of + * this class is significantly higher, at the expense of higher space + * consumption. + * + *

LongAdders can be used with a {@link + * java.util.concurrent.ConcurrentHashMap} to maintain a scalable + * frequency map (a form of histogram or multiset). For example, to + * add a count to a {@code ConcurrentHashMap freqs}, + * initializing if not already present, you can use {@code + * freqs.computeIfAbsent(k -> new LongAdder()).increment();} + * + *

This class extends {@link Number}, but does not define + * methods such as {@code equals}, {@code hashCode} and {@code + * compareTo} because instances are expected to be mutated, and so are + * not useful as collection keys. + * + * @since 1.8 + * @author Doug Lea + */ +public class LongAdder extends Striped64 implements Serializable { + private static final long serialVersionUID = 7249069246863182397L; + + /** + * Creates a new adder with initial sum of zero. + */ + public LongAdder() { + } + + /** + * Adds the given value. + * + * @param x the value to add + */ + public void add(long x) { + //定义cell数组 + Cell[] as; long b, v; int m; Cell a; + //cells为父类striped64为的变量-Cell[],默认为null + //base为父类中的变量-long,默认为0 + //!casBase(b = base, b + x)=>!casBase(0,1)=> + // 即:(成功后,base的值为b+x) + if ((as = cells) != null || !casBase(b = base, b + x)) { + //as不为空或casBase失败了,如果多线程并发大的情况下可能会发生这种情况 + boolean uncontended = true; + //1.a as==null + //1.b ((as.length-1)<0)=>(as.lenght=0) + //2.a (a = as[getProbe() & m])即as数组中随便一个值不为null + //2.b a.cas(v=a.value,v+x)失败,为啥不写成a.cas(a.value,a.value+x) + if (as == null || (m = as.length - 1) < 0 || + (a = as[getProbe() & m]) == null || + !(uncontended = a.cas(v = a.value, v + x))) + //longAccumulate(x,null,true/false),此操作好像较复杂 + longAccumulate(x, null, uncontended); + } + } + + /** + * Handles cases of updates involving initialization, resizing, + * creating new Cells, and/or contention. See above for + * explanation. This method suffers the usual non-modularity + * problems of optimistic retry code, relying on rechecked sets of + * reads. + * + * @param x the value + * @param fn the update function, or null for add (this convention + * avoids the need for an extra field or function in LongAdder). + * @param wasUncontended false if CAS failed before call + */ + final void longAccumulate(long x, LongBinaryOperator fn, + boolean wasUncontended) { + int h; + if ((h = getProbe()) == 0) { + ThreadLocalRandom.current(); // force initialization + h = getProbe(); + wasUncontended = true; + } + boolean collide = false; // True if last slot nonempty + for (;;) { + Cell[] as; Cell a; int n; long v; + if ((as = cells) != null && (n = as.length) > 0) { + //as不为空 + if ((a = as[(n - 1) & h]) == null) { + //a[随机值]==null + if (cellsBusy == 0) { // Try to attach new Cell + //Spinlock (locked via CAS) used when resizing and/or creating Cells. + //transient volatile int cellsBusy; + //x为操作数 + Cell r = new Cell(x); // Optimistically create + //casCellsBusy()会获取锁 + if (cellsBusy == 0 && casCellsBusy()) { + //获取锁成功 + boolean created = false; + try { // Recheck under lock + Cell[] rs; int m, j; + if ((rs = cells) != null && + (m = rs.length) > 0 && + rs[j = (m - 1) & h] == null) { + //在获取到锁后,再次确认后再进行赋值 + rs[j] = r; + created = true; + } + } finally { + //释放锁 + cellsBusy = 0; + } + if (created) + break; + continue; // Slot is now non-empty + } + } + collide = false; + } + else if (!wasUncontended) // CAS already known to fail + wasUncontended = true; // Continue after rehash + else if (a.cas(v = a.value, ((fn == null) ? v + x : + fn.applyAsLong(v, x)))) + //fn为一个接口函数;对cess中的值进行累加 + break; + else if (n >= NCPU || cells != as) + collide = false; // At max size or stale + else if (!collide) + collide = true; + else if (cellsBusy == 0 && casCellsBusy()) { + try { + if (cells == as) { // Expand table unless stale + //Cell为n*2,即进行扩容,每次扩容1倍 + Cell[] rs = new Cell[n << 1]; + for (int i = 0; i < n; ++i) + rs[i] = as[i]; + cells = rs; + } + } finally { + cellsBusy = 0; + } + collide = false; + continue; // Retry with expanded table + } + h = advanceProbe(h); + } + else if (cellsBusy == 0 && cells == as && casCellsBusy()) { + boolean init = false; + try { // Initialize table + if (cells == as) { + // 初始化cells数组大小为2 + Cell[] rs = new Cell[2]; + rs[h & 1] = new Cell(x); + cells = rs; + init = true; + } + } finally { + cellsBusy = 0; + } + if (init) + break; + } + else if (casBase(v = base, ((fn == null) ? v + x : + fn.applyAsLong(v, x)))) + break; // Fall back on using base + } + } + + /** + * Pseudo-randomly advances and records the given probe value for the + * given thread. + * Duplicated from ThreadLocalRandom because of packaging restrictions. + */ + static final int advanceProbe(int probe) { + probe ^= probe << 13; // xorshift + probe ^= probe >>> 17; + probe ^= probe << 5; + UNSAFE.putInt(Thread.currentThread(), PROBE, probe); + return probe; + } + /** + * Returns the probe value for the current thread. + * Duplicated from ThreadLocalRandom because of packaging restrictions. + */ + static final int getProbe() { + //PROBE为一个long类型的变量threadLocalRandomProbe的地址 + //Thread->threadLocalRandomProbe + return UNSAFE.getInt(Thread.currentThread(), PROBE); + } + + /** + * CASes the cellsBusy field from 0 to 1 to acquire lock. + */ + final boolean casCellsBusy() { + return UNSAFE.compareAndSwapInt(this, CELLSBUSY, 0, 1); + } + + /** + * CASes the base field. + */ + final boolean casBase(long cmp, long val) { + //BASE为Striped64中base的内存地址 + return UNSAFE.compareAndSwapLong(this, BASE, cmp, val); + } + + /** + * Equivalent to {@code add(1)}. + */ + public void increment() { + //调用add,进行加1 + add(1L); + } + + /** + * Equivalent to {@code add(-1)}. + */ + public void decrement() { + add(-1L); + } + + /** + * Returns the current sum. The returned value is NOT an + * atomic snapshot; invocation in the absence of concurrent + * updates returns an accurate result, but concurrent updates that + * occur while the sum is being calculated might not be + * incorporated. + * + * @return the sum + */ + public long sum() { + Cell[] as = cells; Cell a; + long sum = base; + if (as != null) { + //cells汇总 + for (int i = 0; i < as.length; ++i) { + if ((a = as[i]) != null) + sum += a.value; + } + } + return sum; + } + + /** + * Resets variables maintaining the sum to zero. This method may + * be a useful alternative to creating a new adder, but is only + * effective if there are no concurrent updates. Because this + * method is intrinsically racy, it should only be used when it is + * known that no threads are concurrently updating. + */ + public void reset() { + Cell[] as = cells; Cell a; + base = 0L; + if (as != null) { + for (int i = 0; i < as.length; ++i) { + if ((a = as[i]) != null) + a.value = 0L; + } + } + } + + /** + * Equivalent in effect to {@link #sum} followed by {@link + * #reset}. This method may apply for example during quiescent + * points between multithreaded computations. If there are + * updates concurrent with this method, the returned value is + * not guaranteed to be the final value occurring before + * the reset. + * + * @return the sum + */ + public long sumThenReset() { + Cell[] as = cells; Cell a; + long sum = base; + base = 0L; + if (as != null) { + for (int i = 0; i < as.length; ++i) { + if ((a = as[i]) != null) { + sum += a.value; + a.value = 0L; + } + } + } + return sum; + } + + /** + * Returns the String representation of the {@link #sum}. + * @return the String representation of the {@link #sum} + */ + public String toString() { + return Long.toString(sum()); + } + + /** + * Equivalent to {@link #sum}. + * + * @return the sum + */ + public long longValue() { + return sum(); + } + + /** + * Returns the {@link #sum} as an {@code int} after a narrowing + * primitive conversion. + */ + public int intValue() { + return (int)sum(); + } + + /** + * Returns the {@link #sum} as a {@code float} + * after a widening primitive conversion. + */ + public float floatValue() { + return (float)sum(); + } + + /** + * Returns the {@link #sum} as a {@code double} after a widening + * primitive conversion. + */ + public double doubleValue() { + return (double)sum(); + } + + /** + * Serialization proxy, used to avoid reference to the non-public + * Striped64 superclass in serialized forms. + * @serial include + */ + private static class SerializationProxy implements Serializable { + private static final long serialVersionUID = 7249069246863182397L; + + /** + * The current value returned by sum(). + * @serial + */ + private final long value; + + SerializationProxy(LongAdder a) { + value = a.sum(); + } + + /** + * Return a {@code LongAdder} object with initial state + * held by this proxy. + * + * @return a {@code LongAdder} object with initial state + * held by this proxy. + */ + private Object readResolve() { + LongAdder a = new LongAdder(); + a.base = value; + return a; + } + } + + /** + * Returns a + * + * SerializationProxy + * representing the state of this instance. + * + * @return a {@link SerializationProxy} + * representing the state of this instance + */ + private Object writeReplace() { + return new SerializationProxy(this); + } + + /** + * @param s the stream + * @throws java.io.InvalidObjectException always + */ + private void readObject(java.io.ObjectInputStream s) + throws java.io.InvalidObjectException { + throw new java.io.InvalidObjectException("Proxy required"); + } + +} +---- +[NOTE] +.通过查看LongAdder的代码中可以看出 +==== +查看源码后可知如下内容: +. LongAdder是为了解决AtomicLong在多线程下更新失败的类 +. LongAdder的实现原理基本上是通过Striped64来实现的,类实现算法类似于jdk1.7中ConcurrentHashmap的实现算法,所谓的分段式来实现的 + + +==== \ No newline at end of file diff --git a/second/week_02/70/Striped64-070.adoc b/second/week_02/70/Striped64-070.adoc index e69de29..a28dbd9 100644 --- a/second/week_02/70/Striped64-070.adoc +++ b/second/week_02/70/Striped64-070.adoc @@ -0,0 +1,400 @@ += Striped64基础入门学习笔记 +Doc Writer +v1.0, 2020-03-15 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- Striped64是干什么的,主要原理是什么? + + +== 部分源码分析笔记 +[source,java] +---- +package java.util.concurrent.atomic; +import java.util.function.LongBinaryOperator; +import java.util.function.DoubleBinaryOperator; +import java.util.concurrent.ThreadLocalRandom; + +/** + * A package-local class holding common representation and mechanics + * for classes supporting dynamic striping on 64bit values. The class + * extends Number so that concrete subclasses must publicly do so. + */ +@SuppressWarnings("serial") +abstract class Striped64 extends Number { + /* + * This class maintains a lazily-initialized table of atomically + * updated variables, plus an extra "base" field. The table size + * is a power of two. Indexing uses masked per-thread hash codes. + * Nearly all declarations in this class are package-private, + * accessed directly by subclasses. + * + * Table entries are of class Cell; a variant of AtomicLong padded + * (via @sun.misc.Contended) to reduce cache contention. Padding + * is overkill for most Atomics because they are usually + * irregularly scattered in memory and thus don't interfere much + * with each other. But Atomic objects residing in arrays will + * tend to be placed adjacent to each other, and so will most + * often share cache lines (with a huge negative performance + * impact) without this precaution. + * + * In part because Cells are relatively large, we avoid creating + * them until they are needed. When there is no contention, all + * updates are made to the base field. Upon first contention (a + * failed CAS on base update), the table is initialized to size 2. + * The table size is doubled upon further contention until + * reaching the nearest power of two greater than or equal to the + * number of CPUS. Table slots remain empty (null) until they are + * needed. + * + * A single spinlock ("cellsBusy") is used for initializing and + * resizing the table, as well as populating slots with new Cells. + * There is no need for a blocking lock; when the lock is not + * available, threads try other slots (or the base). During these + * retries, there is increased contention and reduced locality, + * which is still better than alternatives. + * + * The Thread probe fields maintained via ThreadLocalRandom serve + * as per-thread hash codes. We let them remain uninitialized as + * zero (if they come in this way) until they contend at slot + * 0. They are then initialized to values that typically do not + * often conflict with others. Contention and/or table collisions + * are indicated by failed CASes when performing an update + * operation. Upon a collision, if the table size is less than + * the capacity, it is doubled in size unless some other thread + * holds the lock. If a hashed slot is empty, and lock is + * available, a new Cell is created. Otherwise, if the slot + * exists, a CAS is tried. Retries proceed by "double hashing", + * using a secondary hash (Marsaglia XorShift) to try to find a + * free slot. + * + * The table size is capped because, when there are more threads + * than CPUs, supposing that each thread were bound to a CPU, + * there would exist a perfect hash function mapping threads to + * slots that eliminates collisions. When we reach capacity, we + * search for this mapping by randomly varying the hash codes of + * colliding threads. Because search is random, and collisions + * only become known via CAS failures, convergence can be slow, + * and because threads are typically not bound to CPUS forever, + * may not occur at all. However, despite these limitations, + * observed contention rates are typically low in these cases. + * + * It is possible for a Cell to become unused when threads that + * once hashed to it terminate, as well as in the case where + * doubling the table causes no thread to hash to it under + * expanded mask. We do not try to detect or remove such cells, + * under the assumption that for long-running instances, observed + * contention levels will recur, so the cells will eventually be + * needed again; and for short-lived ones, it does not matter. + */ + + /** + * Padded variant of AtomicLong supporting only raw accesses plus CAS. + * + * JVM intrinsics note: It would be possible to use a release-only + * form of CAS here, if it were provided. + */ + @sun.misc.Contended static final class Cell { + volatile long value; + Cell(long x) { value = x; } + final boolean cas(long cmp, long val) { + return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val); + } + + // Unsafe mechanics + private static final sun.misc.Unsafe UNSAFE; + private static final long valueOffset; + static { + try { + UNSAFE = sun.misc.Unsafe.getUnsafe(); + Class ak = Cell.class; + valueOffset = UNSAFE.objectFieldOffset + (ak.getDeclaredField("value")); + } catch (Exception e) { + throw new Error(e); + } + } + } + + /** Number of CPUS, to place bound on table size */ + static final int NCPU = Runtime.getRuntime().availableProcessors(); + + /** + * Table of cells. When non-null, size is a power of 2. + */ + transient volatile Cell[] cells; + + /** + * Base value, used mainly when there is no contention, but also as + * a fallback during table initialization races. Updated via CAS. + */ + transient volatile long base; + + /** + * Spinlock (locked via CAS) used when resizing and/or creating Cells. + */ + transient volatile int cellsBusy; + + /** + * Package-private default constructor + */ + Striped64() { + } + + /** + * CASes the base field. + */ + final boolean casBase(long cmp, long val) { + return UNSAFE.compareAndSwapLong(this, BASE, cmp, val); + } + + /** + * CASes the cellsBusy field from 0 to 1 to acquire lock. + */ + final boolean casCellsBusy() { + return UNSAFE.compareAndSwapInt(this, CELLSBUSY, 0, 1); + } + + /** + * Returns the probe value for the current thread. + * Duplicated from ThreadLocalRandom because of packaging restrictions. + */ + static final int getProbe() { + return UNSAFE.getInt(Thread.currentThread(), PROBE); + } + + /** + * Pseudo-randomly advances and records the given probe value for the + * given thread. + * Duplicated from ThreadLocalRandom because of packaging restrictions. + */ + static final int advanceProbe(int probe) { + probe ^= probe << 13; // xorshift + probe ^= probe >>> 17; + probe ^= probe << 5; + UNSAFE.putInt(Thread.currentThread(), PROBE, probe); + return probe; + } + + /** + * Handles cases of updates involving initialization, resizing, + * creating new Cells, and/or contention. See above for + * explanation. This method suffers the usual non-modularity + * problems of optimistic retry code, relying on rechecked sets of + * reads. + * + * @param x the value + * @param fn the update function, or null for add (this convention + * avoids the need for an extra field or function in LongAdder). + * @param wasUncontended false if CAS failed before call + */ + final void longAccumulate(long x, LongBinaryOperator fn, + boolean wasUncontended) { + int h; + if ((h = getProbe()) == 0) { + ThreadLocalRandom.current(); // force initialization + h = getProbe(); + wasUncontended = true; + } + boolean collide = false; // True if last slot nonempty + for (;;) { + Cell[] as; Cell a; int n; long v; + if ((as = cells) != null && (n = as.length) > 0) { + if ((a = as[(n - 1) & h]) == null) { + if (cellsBusy == 0) { // Try to attach new Cell + Cell r = new Cell(x); // Optimistically create + if (cellsBusy == 0 && casCellsBusy()) { + boolean created = false; + try { // Recheck under lock + Cell[] rs; int m, j; + if ((rs = cells) != null && + (m = rs.length) > 0 && + rs[j = (m - 1) & h] == null) { + rs[j] = r; + created = true; + } + } finally { + cellsBusy = 0; + } + if (created) + break; + continue; // Slot is now non-empty + } + } + collide = false; + } + else if (!wasUncontended) // CAS already known to fail + wasUncontended = true; // Continue after rehash + else if (a.cas(v = a.value, ((fn == null) ? v + x : + fn.applyAsLong(v, x)))) + break; + else if (n >= NCPU || cells != as) + collide = false; // At max size or stale + else if (!collide) + collide = true; + else if (cellsBusy == 0 && casCellsBusy()) { + try { + if (cells == as) { // Expand table unless stale + Cell[] rs = new Cell[n << 1]; + for (int i = 0; i < n; ++i) + rs[i] = as[i]; + cells = rs; + } + } finally { + cellsBusy = 0; + } + collide = false; + continue; // Retry with expanded table + } + h = advanceProbe(h); + } + else if (cellsBusy == 0 && cells == as && casCellsBusy()) { + boolean init = false; + try { // Initialize table + if (cells == as) { + Cell[] rs = new Cell[2]; + rs[h & 1] = new Cell(x); + cells = rs; + init = true; + } + } finally { + cellsBusy = 0; + } + if (init) + break; + } + else if (casBase(v = base, ((fn == null) ? v + x : + fn.applyAsLong(v, x)))) + break; // Fall back on using base + } + } + + /** + * Same as longAccumulate, but injecting long/double conversions + * in too many places to sensibly merge with long version, given + * the low-overhead requirements of this class. So must instead be + * maintained by copy/paste/adapt. + */ + final void doubleAccumulate(double x, DoubleBinaryOperator fn, + boolean wasUncontended) { + int h; + if ((h = getProbe()) == 0) { + ThreadLocalRandom.current(); // force initialization + h = getProbe(); + wasUncontended = true; + } + boolean collide = false; // True if last slot nonempty + for (;;) { + Cell[] as; Cell a; int n; long v; + if ((as = cells) != null && (n = as.length) > 0) { + if ((a = as[(n - 1) & h]) == null) { + if (cellsBusy == 0) { // Try to attach new Cell + Cell r = new Cell(Double.doubleToRawLongBits(x)); + if (cellsBusy == 0 && casCellsBusy()) { + boolean created = false; + try { // Recheck under lock + Cell[] rs; int m, j; + if ((rs = cells) != null && + (m = rs.length) > 0 && + rs[j = (m - 1) & h] == null) { + rs[j] = r; + created = true; + } + } finally { + cellsBusy = 0; + } + if (created) + break; + continue; // Slot is now non-empty + } + } + collide = false; + } + else if (!wasUncontended) // CAS already known to fail + wasUncontended = true; // Continue after rehash + else if (a.cas(v = a.value, + ((fn == null) ? + Double.doubleToRawLongBits + (Double.longBitsToDouble(v) + x) : + Double.doubleToRawLongBits + (fn.applyAsDouble + (Double.longBitsToDouble(v), x))))) + break; + else if (n >= NCPU || cells != as) + collide = false; // At max size or stale + else if (!collide) + collide = true; + else if (cellsBusy == 0 && casCellsBusy()) { + try { + if (cells == as) { // Expand table unless stale + Cell[] rs = new Cell[n << 1]; + for (int i = 0; i < n; ++i) + rs[i] = as[i]; + cells = rs; + } + } finally { + cellsBusy = 0; + } + collide = false; + continue; // Retry with expanded table + } + h = advanceProbe(h); + } + else if (cellsBusy == 0 && cells == as && casCellsBusy()) { + boolean init = false; + try { // Initialize table + if (cells == as) { + Cell[] rs = new Cell[2]; + rs[h & 1] = new Cell(Double.doubleToRawLongBits(x)); + cells = rs; + init = true; + } + } finally { + cellsBusy = 0; + } + if (init) + break; + } + else if (casBase(v = base, + ((fn == null) ? + Double.doubleToRawLongBits + (Double.longBitsToDouble(v) + x) : + Double.doubleToRawLongBits + (fn.applyAsDouble + (Double.longBitsToDouble(v), x))))) + break; // Fall back on using base + } + } + + // Unsafe mechanics + private static final sun.misc.Unsafe UNSAFE; + private static final long BASE; + private static final long CELLSBUSY; + private static final long PROBE; + static { + try { + UNSAFE = sun.misc.Unsafe.getUnsafe(); + Class sk = Striped64.class; + BASE = UNSAFE.objectFieldOffset + (sk.getDeclaredField("base")); + CELLSBUSY = UNSAFE.objectFieldOffset + (sk.getDeclaredField("cellsBusy")); + Class tk = Thread.class; + PROBE = UNSAFE.objectFieldOffset + (tk.getDeclaredField("threadLocalRandomProbe")); + } catch (Exception e) { + throw new Error(e); + } + } + +} +---- +NOTE: 此类的分析主要在LongAdder中有说明,主要还是分段式来减小锁的粒度来解决高并发问题 \ No newline at end of file -- Gitee