diff --git a/first/week_01/70/ArrayList-070.adoc b/second/week_01/70/ArrayList-070.adoc similarity index 100% rename from first/week_01/70/ArrayList-070.adoc rename to second/week_01/70/ArrayList-070.adoc diff --git a/first/week_01/70/HashMap-070.adoc b/second/week_01/70/HashMap-070.adoc similarity index 100% rename from first/week_01/70/HashMap-070.adoc rename to second/week_01/70/HashMap-070.adoc diff --git a/first/week_01/70/LinkedList-070.adoc b/second/week_01/70/LinkedList-070.adoc similarity index 100% rename from first/week_01/70/LinkedList-070.adoc rename to second/week_01/70/LinkedList-070.adoc diff --git a/first/week_01/70/README.md b/second/week_01/70/README.md similarity index 100% rename from first/week_01/70/README.md rename to second/week_01/70/README.md diff --git a/second/week_02/70/AtomicInteger-070.adoc b/second/week_02/70/AtomicInteger-070.adoc new file mode 100644 index 0000000000000000000000000000000000000000..26ee3fc5129754bb8e34f4fda7f86343c0778101 --- /dev/null +++ b/second/week_02/70/AtomicInteger-070.adoc @@ -0,0 +1,154 @@ += AtomicInteger基础入门学习笔记 +Doc Writer +v1.0, 2020-03-13 +:toc-title: 目录 +:toc: left + +== 基本要求 +=== 源码学习要求 +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 +jdk1.8 + +=== 学习后需要至少掌握的知识点 +- AtomicInteger与Integer有什么区别? +- 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; + } + +} +---- + + + +== 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】【小特】没有感冒过,不需要进行隔离 +---- diff --git a/second/week_02/70/AtomicStampedReference-070.adoc b/second/week_02/70/AtomicStampedReference-070.adoc new file mode 100644 index 0000000000000000000000000000000000000000..705ddb19fe31a69ed0a9e2ea1f8608dcca713dcf --- /dev/null +++ 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/LongAdder-070.adoc b/second/week_02/70/LongAdder-070.adoc new file mode 100644 index 0000000000000000000000000000000000000000..ddfc98473c801501a885b636fac98f622889bc17 --- /dev/null +++ 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 new file mode 100644 index 0000000000000000000000000000000000000000..a28dbd9ad150576c6b30a052a57aeecaf5c0b3b3 --- /dev/null +++ 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 diff --git a/second/week_02/70/Unsafe-070.adoc b/second/week_02/70/Unsafe-070.adoc new file mode 100644 index 0000000000000000000000000000000000000000..5098685b4ed4b0aa6805b323c92bf7d1e563636e --- /dev/null +++ b/second/week_02/70/Unsafe-070.adoc @@ -0,0 +1,147 @@ += Unsage基础入门学习笔记 +Doc Writer +v1.0, 2020-03-09 +:toc-title: 目录 +:toc: left + +== 基本要求 + +=== 源码学习要求 + +- 至少提交2个类的源码分析笔记 + +=== jdk环境要求 + +jdk1.8 + +=== 学习后需要至少掌握的知识点 + +- Unsafe有什么用? +- Unsafe如何调用? + +== 部分源码分析笔记 + +=== 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); +---- + +=== Unsafe的常见方法 + +[source,adoc] +---- + 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); + + public native void unpark(Object var1); + + public native void park(boolean var1, long var2); +---- + +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; + } +---- + +NOTE: 查看了数据cas部分的代码并通过自己的简单实践,感觉Unsafe中的许多方法便就是oop-class模型的入口方法, 通过此类的源码学习,对oop-class模型有了更进行一步的了解