JDK早期,synchronized 叫做重量级锁, 因为申请锁资源必须通过kernel, 系统调用

  1. ;hello.asm
  2. ;write(int fd, const void *buffer, size_t nbytes)
  3. section data
  4. msg db "Hello", 0xA
  5. len equ $ - msg
  6. section .text
  7. global _start
  8. _start:
  9. mov edx, len
  10. mov ecx, msg
  11. mov ebx, 1 ;文件描述符1 std_out
  12. mov eax, 4 ;write函数系统调用号 4
  13. int 0x80
  14. mov ebx, 0
  15. mov eax, 1 ;exit函数系统调用号
  16. int 0x80

CAS

Compare And Swap (Compare And Exchange) / 自旋 / 自旋锁 / 无锁 (无重量锁)

因为经常配合循环操作,直到完成为止,所以泛指一类操作

cas(v, a, b) ,变量v,期待值a, 修改值b

ABA问题,你的女朋友在离开你的这段儿时间经历了别的人,自旋就是你空转等待,一直等到她接纳你为止

解决办法(版本号 AtomicStampedReference),基础类型简单值不需要版本号

Unsafe

AtomicInteger:

  1. public final int incrementAndGet() {
  2. for (;;) {
  3. int current = get();
  4. int next = current + 1;
  5. if (compareAndSet(current, next))
  6. return next;
  7. }
  8. }
  9. public final boolean compareAndSet(int expect, int update) {
  10. return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
  11. }

Unsafe:

  1. public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5);

运用:

  1. package com.mashibing.jol;
  2. import sun.misc.Unsafe;
  3. import java.lang.reflect.Field;
  4. public class T02_TestUnsafe {
  5. int i = 0;
  6. private static T02_TestUnsafe t = new T02_TestUnsafe();
  7. public static void main(String[] args) throws Exception {
  8. //Unsafe unsafe = Unsafe.getUnsafe();
  9. Field unsafeField = Unsafe.class.getDeclaredFields()[0];
  10. unsafeField.setAccessible(true);
  11. Unsafe unsafe = (Unsafe) unsafeField.get(null);
  12. Field f = T02_TestUnsafe.class.getDeclaredField("i");
  13. long offset = unsafe.objectFieldOffset(f);
  14. System.out.println(offset);
  15. boolean success = unsafe.compareAndSwapInt(t, offset, 0, 1);
  16. System.out.println(success);
  17. System.out.println(t.i);
  18. //unsafe.compareAndSwapInt()
  19. }
  20. }

jdk8u: unsafe.cpp:

cmpxchg = compare and exchange

  1. UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
  2. UnsafeWrapper("Unsafe_CompareAndSwapInt");
  3. oop p = JNIHandles::resolve(obj);
  4. jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);
  5. return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
  6. UNSAFE_END

jdk8u: atomic_linux_x86.inline.hpp 93行

is_MP = Multi Processor

  1. inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) {
  2. int mp = os::is_MP();
  3. __asm__ volatile (LOCK_IF_MP(%4) "cmpxchgl %1,(%3)"
  4. : "=a" (exchange_value)
  5. : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp)
  6. : "cc", "memory");
  7. return exchange_value;
  8. }

jdk8u: os.hpp is_MP()

  1. static inline bool is_MP() {
  2. // During bootstrap if _processor_count is not yet initialized
  3. // we claim to be MP as that is safest. If any platform has a
  4. // stub generator that might be triggered in this phase and for
  5. // which being declared MP when in fact not, is a problem - then
  6. // the bootstrap routine for the stub generator needs to check
  7. // the processor count directly and leave the bootstrap routine
  8. // in place until called after initialization has ocurred.
  9. return (_processor_count != 1) || AssumeMP;
  10. }

jdk8u: atomic_linux_x86.inline.hpp

  1. #define LOCK_IF_MP(mp) "cmp $0, " #mp "; je 1f; lock; 1: "

最终实现:

cmpxchg = cas修改变量值

  1. lock cmpxchg 指令

硬件:

lock指令在执行后面指令的时候锁定一个北桥信号

(不采用锁总线的方式)

markword

工具:JOL = Java Object Layout

  1. <dependencies>
  2. <!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
  3. <dependency>
  4. <groupId>org.openjdk.jol</groupId>
  5. <artifactId>jol-core</artifactId>
  6. <version>0.9</version>
  7. </dependency>
  8. </dependencies>

jdk8u: markOop.hpp

  1. // Bit-format of an object header (most significant first, big endian layout below):
  2. //
  3. // 32 bits:
  4. // --------
  5. // hash:25 ------------>| age:4 biased_lock:1 lock:2 (normal object)
  6. // JavaThread*:23 epoch:2 age:4 biased_lock:1 lock:2 (biased object)
  7. // size:32 ------------------------------------------>| (CMS free block)
  8. // PromotedObject*:29 ---------->| promo_bits:3 ----->| (CMS promoted object)
  9. //
  10. // 64 bits:
  11. // --------
  12. // unused:25 hash:31 -->| unused:1 age:4 biased_lock:1 lock:2 (normal object)
  13. // JavaThread*:54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased object)
  14. // PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)
  15. // size:64 ----------------------------------------------------->| (CMS free block)
  16. //
  17. // unused:25 hash:31 -->| cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && normal object)
  18. // JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && biased object)
  19. // narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object)
  20. // unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)

synchronized的横切面详解

  1. synchronized原理
  2. 升级过程
  3. 汇编实现
  4. vs reentrantLock的区别

java源码层级

synchronized(o)

字节码层级

monitorenter moniterexit

JVM层级(Hotspot)

  1. package com.mashibing.insidesync;
  2. import org.openjdk.jol.info.ClassLayout;
  3. public class T01_Sync1 {
  4. public static void main(String[] args) {
  5. Object o = new Object();
  6. System.out.println(ClassLayout.parseInstance(o).toPrintable());
  7. }
  8. }
  1. com.mashibing.insidesync.T01_Sync1$Lock object internals:
  2. OFFSET SIZE TYPE DESCRIPTION VALUE
  3. 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5)
  4. 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
  5. 8 4 (object header) 49 ce 00 20 (01001001 11001110 00000000 00100000) (536923721)
  6. 12 4 (loss due to the next object alignment)
  7. Instance size: 16 bytes
  8. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
  1. com.mashibing.insidesync.T02_Sync2$Lock object internals:
  2. OFFSET SIZE TYPE DESCRIPTION VALUE
  3. 0 4 (object header) 05 90 2e 1e (00000101 10010000 00101110 00011110) (506368005)
  4. 4 4 (object header) 1b 02 00 00 (00011011 00000010 00000000 00000000) (539)
  5. 8 4 (object header) 49 ce 00 20 (01001001 11001110 00000000 00100000) (536923721)
  6. 12 4 (loss due to the next object alignment)
  7. Instance size: 16 bytes
  8. Space losses: 0 bytes internal + 4 bytes external = 4 bytes tota

InterpreterRuntime:: monitorenter方法

  1. IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
  2. #ifdef ASSERT
  3. thread->last_frame().interpreter_frame_verify_monitor(elem);
  4. #endif
  5. if (PrintBiasedLockingStatistics) {
  6. Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
  7. }
  8. Handle h_obj(thread, elem->obj());
  9. assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
  10. "must be NULL or an object");
  11. if (UseBiasedLocking) {
  12. // Retry fast entry if bias is revoked to avoid unnecessary inflation
  13. ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
  14. } else {
  15. ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
  16. }
  17. assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
  18. "must be NULL or an object");
  19. #ifdef ASSERT
  20. thread->last_frame().interpreter_frame_verify_monitor(elem);
  21. #endif
  22. IRT_END

synchronizer.cpp

revoke_and_rebias

  1. void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) {
  2. if (UseBiasedLocking) {
  3. if (!SafepointSynchronize::is_at_safepoint()) {
  4. BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
  5. if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
  6. return;
  7. }
  8. } else {
  9. assert(!attempt_rebias, "can not rebias toward VM thread");
  10. BiasedLocking::revoke_at_safepoint(obj);
  11. }
  12. assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
  13. }
  14. slow_enter (obj, lock, THREAD) ;
  15. }
  1. void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
  2. markOop mark = obj->mark();
  3. assert(!mark->has_bias_pattern(), "should not see bias pattern here");
  4. if (mark->is_neutral()) {
  5. // Anticipate successful CAS -- the ST of the displaced mark must
  6. // be visible <= the ST performed by the CAS.
  7. lock->set_displaced_header(mark);
  8. if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {
  9. TEVENT (slow_enter: release stacklock) ;
  10. return ;
  11. }
  12. // Fall through to inflate() ...
  13. } else
  14. if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
  15. assert(lock != mark->locker(), "must not re-lock the same lock");
  16. assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
  17. lock->set_displaced_header(NULL);
  18. return;
  19. }
  20. #if 0
  21. // The following optimization isn't particularly useful.
  22. if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {
  23. lock->set_displaced_header (NULL) ;
  24. return ;
  25. }
  26. #endif
  27. // The object header will never be displaced to this lock,
  28. // so it does not matter what the value is, except that it
  29. // must be non-zero to avoid looking like a re-entrant lock,
  30. // and must not look locked either.
  31. lock->set_displaced_header(markOopDesc::unused_mark());
  32. ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
  33. }

inflate方法:膨胀为重量级锁

锁升级过程

JDK8 markword实现表:

用户态与内核态 - 图1

new - 偏向锁 - 轻量级锁 (无锁, 自旋锁,自适应自旋)- 重量级锁

synchronized优化的过程和markword息息相关

用markword中最低的三位代表锁状态 其中1位是偏向锁位 两位是普通锁位

  1. Object o = new Object()
    锁 = 0 01 无锁态
    注意:如果偏向锁打开,默认是匿名偏向状态

  2. o.hashCode()
    001 + hashcode

    1. 00000001 10101101 00110100 00110110
    2. 01011001 00000000 00000000 00000000


little endian big endian
00000000 00000000 00000000 01011001 00110110 00110100 10101101 00000000

  1. 默认synchronized(o)
    00 -> 轻量级锁
    默认情况 偏向锁有个时延,默认是4秒
    why? 因为JVM虚拟机自己有一些默认启动的线程,里面有好多sync代码,这些sync代码启动时就知道肯定会有竞争,如果使用偏向锁,就会造成偏向锁不断的进行锁撤销和锁升级的操作,效率较低。
    1. -XX:BiasedLockingStartupDelay=0
  1. 如果设定上述参数
    new Object () - > 101 偏向锁 ->线程ID为0 -> Anonymous BiasedLock
    打开偏向锁,new出来的对象,默认就是一个可偏向匿名对象101

  2. 如果有线程上锁
    上偏向锁,指的就是,把markword的线程ID改为自己线程ID的过程
    偏向锁不可重偏向 批量偏向 批量撤销

  3. 如果有线程竞争
    撤销偏向锁,升级轻量级锁
    线程在自己的线程栈生成LockRecord ,用CAS操作将markword设置为指向自己这个线程的LR的指针,设置成功者得到锁

  4. 如果竞争加剧
    竞争加剧:有线程超过10次自旋, -XX:PreBlockSpin, 或者自旋线程数超过CPU核数的一半, 1.6之后,加入自适应自旋 Adapative Self Spinning , JVM自己控制
    升级重量级锁:-> 向操作系统申请资源,linux mutex , CPU从3级-0级系统调用,线程挂起,进入等待队列,等待操作系统的调度,然后再映射回用户空间

(以上实验环境是JDK11,打开就是偏向锁,而JDK8默认对象头是无锁)

偏向锁默认是打开的,但是有一个时延,如果要观察到偏向锁,应该设定参数

如果计算过对象的hashCode,则对象无法进入偏向状态!

轻量级锁重量级锁的hashCode存在与什么地方?

答案:线程栈中,轻量级锁的LR中,或是代表重量级锁的ObjectMonitor的成员中

关于epoch: (不重要)

批量重偏向与批量撤销渊源:从偏向锁的加锁解锁过程中可看出,当只有一个线程反复进入同步块时,偏向锁带来的性能开销基本可以忽略,但是当有其他线程尝试获得锁时,就需要等到safe point时,再将偏向锁撤销为无锁状态或升级为轻量级,会消耗一定的性能,所以在多线程竞争频繁的情况下,偏向锁不仅不能提高性能,还会导致性能下降。于是,就有了批量重偏向与批量撤销的机制。

原理以class为单位,为每个class维护解决场景批量重偏向(bulk rebias)机制是为了解决:一个线程创建了大量对象并执行了初始的同步操作,后来另一个线程也来将这些对象作为锁对象进行操作,这样会导致大量的偏向锁撤销操作。批量撤销(bulk revoke)机制是为了解决:在明显多线程竞争剧烈的场景下使用偏向锁是不合适的。

一个偏向锁撤销计数器,每一次该class的对象发生偏向撤销操作时,该计数器+1,当这个值达到重偏向阈值(默认20)时,JVM就认为该class的偏向锁有问题,因此会进行批量重偏向。每个class对象会有一个对应的epoch字段,每个处于偏向锁状态对象的Mark Word中也有该字段,其初始值为创建该对象时class中的epoch的值。每次发生批量重偏向时,就将该值+1,同时遍历JVM中所有线程的栈,找到该class所有正处于加锁状态的偏向锁,将其epoch字段改为新值。下次获得锁时,发现当前对象的epoch值和class的epoch不相等,那就算当前已经偏向了其他线程,也不会执行撤销操作,而是直接通过CAS操作将其Mark Word的Thread Id 改成当前线程Id。当达到重偏向阈值后,假设该class计数器继续增长,当其达到批量撤销的阈值后(默认40),JVM就认为该class的使用场景存在多线程竞争,会标记该class为不可偏向,之后,对于该class的锁,直接走轻量级锁的逻辑。

没错,我就是厕所所长

加锁,指的是锁定对象

锁升级的过程

JDK较早的版本 OS的资源 互斥量 用户态 -> 内核态的转换 重量级 效率比较低

现代版本进行了优化

无锁 - 偏向锁 -轻量级锁(自旋锁)-重量级锁

偏向锁 - markword 上记录当前线程指针,下次同一个线程加锁的时候,不需要争用,只需要判断线程指针是否同一个,所以,偏向锁,偏向加锁的第一个线程 。hashCode备份在线程栈上 线程销毁,锁降级为无锁

有争用 - 锁升级为轻量级锁 - 每个线程有自己的LockRecord在自己的线程栈上,用CAS去争用markword的LR的指针,指针指向哪个线程的LR,哪个线程就拥有锁

自旋超过10次,升级为重量级锁 - 如果太多线程自旋 CPU消耗过大,不如升级为重量级锁,进入等待队列(不消耗CPU)-XX:PreBlockSpin

自旋锁在 JDK1.4.2 中引入,使用 -XX:+UseSpinning 来开启。JDK 6 中变为默认开启,并且引入了自适应的自旋锁(适应性自旋锁)。

自适应自旋锁意味着自旋的时间(次数)不再固定,而是由前一次在同一个锁上的自旋时间及锁的拥有者的状态来决定。如果在同一个锁对象上,自旋等待刚刚成功获得过锁,并且持有锁的线程正在运行中,那么虚拟机就会认为这次自旋也是很有可能再次成功,进而它将允许自旋等待持续相对更长的时间。如果对于某个锁,自旋很少成功获得过,那在以后尝试获取这个锁时将可能省略掉自旋过程,直接阻塞线程,避免浪费处理器资源。

偏向锁由于有锁撤销的过程revoke,会消耗系统资源,所以,在锁争用特别激烈的时候,用偏向锁未必效率高。还不如直接使用轻量级锁。

synchronized最底层实现

  1. public class T {
  2. static volatile int i = 0;
  3. public static void n() { i++; }
  4. public static synchronized void m() {}
  5. publics static void main(String[] args) {
  6. for(int j=0; j<1000_000; j++) {
  7. m();
  8. n();
  9. }
  10. }
  11. }

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly T

C1 Compile Level 1 (一级优化)

C2 Compile Level 2 (二级优化)

找到m() n()方法的汇编码,会看到 lock comxchg …..指令

synchronized vs Lock (CAS)

  1. 在高争用 高耗时的环境下synchronized效率更高
  2. 在低争用 低耗时的环境下CAS效率更高
  3. synchronized到重量级之后是等待队列(不消耗CPU
  4. CAS(等待期间消耗CPU
  5. 一切以实测为准

锁消除 lock eliminate

  1. public void add(String str1,String str2){
  2. StringBuffer sb = new StringBuffer();
  3. sb.append(str1).append(str2);
  4. }

我们都知道 StringBuffer 是线程安全的,因为它的关键方法都是被 synchronized 修饰过的,但我们看上面这段代码,我们会发现,sb 这个引用只会在 add 方法中使用,不可能被其它线程引用(因为是局部变量,栈私有),因此 sb 是不可能共享的资源,JVM 会自动消除 StringBuffer 对象内部的锁。

锁粗化 lock coarsening

  1. public String test(String str){
  2. int i = 0;
  3. StringBuffer sb = new StringBuffer():
  4. while(i < 100){
  5. sb.append(str);
  6. i++;
  7. }
  8. return sb.toString():
  9. }

JVM 会检测到这样一连串的操作都对同一个对象加锁(while 循环内 100 次执行 append,没有锁粗化的就要进行 100 次加锁/解锁),此时 JVM 就会将加锁的范围粗化到这一连串的操作的外部(比如 while 虚幻体外),使得这一连串操作只需要加一次锁即可。

锁降级(不重要)

https://www.zhihu.com/question/63859501

其实,只被VMThread访问,降级也就没啥意义了。所以可以简单认为锁降级不存在!

超线程

一个ALU + 两组Registers + PC

参考资料

http://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html

volatile的用途

1.线程可见性

  1. package com.mashibing.testvolatile;
  2. public class T01_ThreadVisibility {
  3. private static volatile boolean flag = true;
  4. public static void main(String[] args) throws InterruptedException {
  5. new Thread(()-> {
  6. while (flag) {
  7. //do sth
  8. }
  9. System.out.println("end");
  10. }, "server").start();
  11. Thread.sleep(1000);
  12. flag = false;
  13. }
  14. }

2.防止指令重排序

问题:DCL单例需不需要加volatile?

CPU的基础知识

  • 缓存行对齐
    缓存行64个字节是CPU同步的基本单位,缓存行隔离会比伪共享效率要高
    Disruptor ```java package com.mashibing.juc.c_028_FalseSharing;

public class T02_CacheLinePadding { private static class Padding { public volatile long p1, p2, p3, p4, p5, p6, p7; // }

  1. private static class T extends Padding {
  2. public volatile long x = 0L;
  3. }
  4. public static T[] arr = new T[2];
  5. static {
  6. arr[0] = new T();
  7. arr[1] = new T();
  8. }
  9. public static void main(String[] args) throws Exception {
  10. Thread t1 = new Thread(()->{
  11. for (long i = 0; i < 1000_0000L; i++) {
  12. arr[0].x = i;
  13. }
  14. });
  15. Thread t2 = new Thread(()->{
  16. for (long i = 0; i < 1000_0000L; i++) {
  17. arr[1].x = i;
  18. }
  19. });
  20. final long start = System.nanoTime();
  21. t1.start();
  22. t2.start();
  23. t1.join();
  24. t2.join();
  25. System.out.println((System.nanoTime() - start)/100_0000);
  26. }

}

  1. <br />MESI
  2. -
  3. 伪共享
  4. -
  5. 合并写<br />
  6. CPU内部的4个字节的Buffer
  7. ```java
  8. package com.mashibing.juc.c_029_WriteCombining;
  9. public final class WriteCombining {
  10. private static final int ITERATIONS = Integer.MAX_VALUE;
  11. private static final int ITEMS = 1 << 24;
  12. private static final int MASK = ITEMS - 1;
  13. private static final byte[] arrayA = new byte[ITEMS];
  14. private static final byte[] arrayB = new byte[ITEMS];
  15. private static final byte[] arrayC = new byte[ITEMS];
  16. private static final byte[] arrayD = new byte[ITEMS];
  17. private static final byte[] arrayE = new byte[ITEMS];
  18. private static final byte[] arrayF = new byte[ITEMS];
  19. public static void main(final String[] args) {
  20. for (int i = 1; i <= 3; i++) {
  21. System.out.println(i + " SingleLoop duration (ns) = " + runCaseOne());
  22. System.out.println(i + " SplitLoop duration (ns) = " + runCaseTwo());
  23. }
  24. }
  25. public static long runCaseOne() {
  26. long start = System.nanoTime();
  27. int i = ITERATIONS;
  28. while (--i != 0) {
  29. int slot = i & MASK;
  30. byte b = (byte) i;
  31. arrayA[slot] = b;
  32. arrayB[slot] = b;
  33. arrayC[slot] = b;
  34. arrayD[slot] = b;
  35. arrayE[slot] = b;
  36. arrayF[slot] = b;
  37. }
  38. return System.nanoTime() - start;
  39. }
  40. public static long runCaseTwo() {
  41. long start = System.nanoTime();
  42. int i = ITERATIONS;
  43. while (--i != 0) {
  44. int slot = i & MASK;
  45. byte b = (byte) i;
  46. arrayA[slot] = b;
  47. arrayB[slot] = b;
  48. arrayC[slot] = b;
  49. }
  50. i = ITERATIONS;
  51. while (--i != 0) {
  52. int slot = i & MASK;
  53. byte b = (byte) i;
  54. arrayD[slot] = b;
  55. arrayE[slot] = b;
  56. arrayF[slot] = b;
  57. }
  58. return System.nanoTime() - start;
  59. }
  60. }
  • 指令重排序 ```java package com.mashibing.jvm.c3_jmm;

public class T04_Disorder { private static int x = 0, y = 0; private static int a = 0, b =0;

  1. public static void main(String[] args) throws InterruptedException {
  2. int i = 0;
  3. for(;;) {
  4. i++;
  5. x = 0; y = 0;
  6. a = 0; b = 0;
  7. Thread one = new Thread(new Runnable() {
  8. public void run() {
  9. //由于线程one先启动,下面这句话让它等一等线程two. 读着可根据自己电脑的实际性能适当调整等待时间.
  10. //shortWait(100000);
  11. a = 1;
  12. x = b;
  13. }
  14. });
  15. Thread other = new Thread(new Runnable() {
  16. public void run() {
  17. b = 1;
  18. y = a;
  19. }
  20. });
  21. one.start();other.start();
  22. one.join();other.join();
  23. String result = "第" + i + "次 (" + x + "," + y + ")";
  24. if(x == 0 && y == 0) {
  25. System.err.println(result);
  26. break;
  27. } else {
  28. //System.out.println(result);
  29. }
  30. }
  31. }
  32. public static void shortWait(long interval){
  33. long start = System.nanoTime();
  34. long end;
  35. do{
  36. end = System.nanoTime();
  37. }while(start + interval >= end);
  38. }

}

  1. <a name="2bc27e3e"></a>
  2. ### 系统底层如何实现数据一致性
  3. 1. MESI如果能解决,就使用MESI
  4. 2. 如果不能,就锁总线
  5. <a name="3e828f0f"></a>
  6. ### 系统底层如何保证有序性
  7. 1. 内存屏障sfence mfence lfence等系统原语
  8. 2. 锁总线
  9. <a name="70ac7c22"></a>
  10. ### volatile如何解决指令重排序
  11. 1: volatile i
  12. 2: ACC_VOLATILE
  13. 3: JVM的内存屏障
  14. 屏障两边的指令不可以重排!保障有序!
  15. 4:hotspot实现
  16. bytecodeinterpreter.cpp
  17. ```c
  18. int field_offset = cache->f2_as_index();
  19. if (cache->is_volatile()) {
  20. if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
  21. OrderAccess::fence();
  22. }

orderaccess_linux_x86.inline.hpp

  1. inline void OrderAccess::fence() {
  2. if (os::is_MP()) {
  3. // always use locked addl since mfence is sometimes expensive
  4. #ifdef AMD64
  5. __asm__ volatile ("lock; addl $0,0(%%rsp)" : : : "cc", "memory");
  6. #else
  7. __asm__ volatile ("lock; addl $0,0(%%esp)" : : : "cc", "memory");
  8. #endif
  9. }
  10. }