注意:JEP142規範,Reduce Cache Contention on Specified Fields。java
僞分享的狀況下,可使用填充和JDK8的@Contended註解。緩存
可是實驗結果證實數據填充並不能作的很好,由於不一樣的機器、不一樣的操做系統對緩存行的使用狀況不同,咱們很難肯定咱們機器上的緩存使用機制就是如咱們設想的那樣,因此建議使用JDK8的@Contended註解。dom
爲何偏向於使用@Contended註解:jvm
List-1 Intellij Idea中打開的Oracle JDK @Contended沒有註釋ide
package sun.misc; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) public @interface Contended { String value() default ""; }
List-2 來看openJDK的@Contended註解,源碼地址fetch
...這裏有不少註釋,自行查看源碼... @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) public @interface Contended { /** * The (optional) contention group tag. * This tag is only meaningful for field level annotations. * * @return contention group tag. */ String value() default ""; }
openJDK的Contended.java中有詳細的註釋,這個value只有在類屬性上纔有意義,表示"contention group tag",理解字面意思,可是沒有理解具體含義。查google,查到Stackoverflow上別人的回覆,說的和List-2註釋差很少,理解字面意思,可是沒理解含義,原文地址。我把別人的回答截圖,以下圖1。ui
圖1 Stackoverflow上關於@Contended的value回答this
ForkjoinPool的內部類WorkQueuegoogle
圖1 JDK8中ForkjoinPool的WorkQueuespa
List-3 JDK8中Thread內部屬性,這幾個屬性與ThreadLocalRandom有關
// The following three initially uninitialized fields are exclusively // managed by class java.util.concurrent.ThreadLocalRandom. These // fields are used to build the high-performance PRNGs in the // concurrent code, and we can not risk accidental false sharing. // Hence, the fields are isolated with @Contended. /** The current seed for a ThreadLocalRandom */ @sun.misc.Contended("tlr") long threadLocalRandomSeed; /** Probe hash value; nonzero if threadLocalRandomSeed initialized */ @sun.misc.Contended("tlr") int threadLocalRandomProbe; /** Secondary seed isolated from public ThreadLocalRandom sequence */ @sun.misc.Contended("tlr") int threadLocalRandomSecondarySeed;
List-4 JDK8中Striped64的內部類Cell
/** * 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); } } }
在這裏就只是舉這幾個例子