Unsafe

http://www.cnblogs.com/mickole/articles/3757278.html

unsafe类里面是大量的native方法,主要是一些对内存的直接操作等。

unsafe.park is pretty much the same as thread.wait, except that it's using architecture specific code (thus the reason it's 'unsafe'). unsafe is not made available publicly, but is used within java internal libraries where architecture specific code would offer significant optimization benefits. It's used a lot for thread pooling.

Unsafe类提供了硬件级别的原子操作,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能。具体实现使用c++,详见文件sun.misc.natUnsafe.cc();

CASvolatile的读和写的内存语义:1、禁止任何的读写重排序

                                                          2、数据更新到主存

                                                          3、原子性操作

                                                          4、volitale的读语义好像没有体现出来,因为书中只说到指令会强制刷新内存,没有说会使得本地缓存失效。

CAS的实现:

   本质是lock指令:1、禁止指令重排序(读写都禁止)

                         2、写操作刷新主存

                         3、锁住总线来保证指令执行的原子性。当然也保证了互斥性。现在可以使用缓存锁定来保证指令执行的原子性,可以大大降低锁总线的开销。

原文地址:https://www.cnblogs.com/YDDMAX/p/5635125.html