1 CAS(Compare And Swap)
1.1 什么是CAS?
CAS:Compare And Swap 比较与转换,是一个同步原语。底层的原理就是使用了CPU的指令,通过硬件来实现的,具有原子性的特征。CAS(oldValue, exceptedValue) 比较期望值与旧值,如果不相同就替换掉,因为CAS是原子性的,所有多线程操作的时候,不需要使用其他方式来保证原子性,直接使用即可。原子类大量使用到了CAS,例如:
static class IntCount { static int i = 0; public static synchronized void incr(){ i ++; } static AtomicInteger count = new AtomicInteger(1); public static void incr1(){ count.addAndGet(1); } }
1.2 常见问题
1.2.1 ABA问题
ABA问题,CAS是通过比较期望值与旧值,如果不相同那么就变更,否则不变更。
假设:某个数据的初始值为A;线程1打算将A->C,线程2打算A->B,B->A
当开始的时候线程1,和线程2都拿到的是A,这个时候假设线程1阻塞了,然后线程2执行完了A->B, B->A,线程2执行结束之后数据还是A,这个时候线程1读取到的就是A,因此修改成了C,但是实际上A->C的过程经历了线程2的A->B-A的过程,中间的操作被线程1给忽略了;
如何解决这个问题呢?一般有2中方法:增加版本号、增加时间戳;以原子类为例子:
1、AtomicInteger 是普通的CAS类;
2、AtomicStampedReference 是可以增加时间戳和版本号解决的;
以上述线程1和线程2为例,如果线程1要修改A->C,还会增加一个条件版本号为1,但是如果经历了线程2的A->B->A的过程,此时版本号已经变成了3(修改了2次),那么线程1再次修改的时候版本号不一致,表示虽然值为A都是不是最开始的那个状态下的A,因此更新就会失败。
1.2.2 占用CPU的问题
CAS会通常使用到自旋锁的场景,就是暂停一段时间,然后尝试去获取执行权限。但是如果长期获取不到,就会出现占用大量的CPU; 所以一般会设置自旋的次数。
1.2.3 如何保证多个共享变量的原子操作
AtomicReference可以对对象进行原子性的CAS操作
2 Unsafe类
Unsafe是sum.misc包下的一个类,主要提供一些用于执行低级别、不安全操作的方法,比如:直接访问系统内存资源、自主管理内存资源,这些方法执行效率会比较高,并且增加了java语言对底层资源的操操作能力。有点类似于c语言一样,可以直接操作内存空间的能力,那么就会出现类似c语言的问题,指针带来的风险拥有直接操作系统的权限,那么就需要使用过程中注意。
Unsafe大致提供如下:
2.1 Unsafe Cas
Unsafe 针对CAS提供了三个方法:(都是JNI方法,也就是都是c实现的)
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);
1、compareAndSwap*的方法底层是如何实现:
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) UnsafeWrapper("Unsafe_CompareAndSwapInt"); oop p = JNIHandles::resolve(obj); jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); // 使用到了Atomic::cmpxchg指令 return (jint)(Atomic::cmpxchg(x, addr, e)) == e; UNSAFE_END
2、在看看Atomic::cmpxchg指令是如何实现在操作系统中(以:Linux x86为例)
inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { int mp = os::is_MP(); // 增加了lock前缀,目的是在多核的情况下增加内存屏障 __asm__ volatile (LOCK_IF_MP(%4) "cmpxchgl %1,(%3)" : "=a" (exchange_value) : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp) : "cc", "memory"); return exchange_value; }
3、cmpxchg函数
如果ptr和old的值一样,则把new写到ptr内存,否则返回ptr的值,执行是原子性的。cmpxchg(void* ptr, int old, int new)
4,java8后增加了:
public final int getAndAddInt(Object o, long offset, int delta) { int v; do { v = getIntVolatile(o, offset); } while (!compareAndSwapInt(o, offset, v, v + delta)); return v; } public final long getAndAddLong(Object o, long offset, long delta) { long v; do { v = getLongVolatile(o, offset); } while (!compareAndSwapLong(o, offset, v, v + delta)); return v; } public final int getAndSetInt(Object o, long offset, int newValue) { int v; do { v = getIntVolatile(o, offset); } while (!compareAndSwapInt(o, offset, v, newValue)); return v; } public final long getAndSetLong(Object o, long offset, long newValue) { long v; do { v = getLongVolatile(o, offset); } while (!compareAndSwapLong(o, offset, v, newValue)); return v; } public final Object getAndSetObject(Object o, long offset, Object newValue) { Object v; do { v = getObjectVolatile(o, offset); } while (!compareAndSwapObject(o, offset, v, newValue)); return v;
2.2 Unsafe 线程(锁)
1、锁
如下的几个方法,monitorEnter和moniterExit就是同步关键字的实现底层原理,不过这些方法基本都变成了过期的类型,因此不推荐使用。
/** @deprecated */ @Deprecated public native void monitorEnter(Object var1); /** @deprecated */ @Deprecated public native void monitorExit(Object var1); /** @deprecated */ @Deprecated public native boolean tryMonitorEnter(Object var1);
2、挂起线程和恢复线程
// 恢复线程 public native void unpark(Object var1); // 挂起线程 public native void park(boolean var1, long var2);
(1)park实现原理
这也是LockSupport类中的park和unpark的内部实现逻辑,那么他的原理是啥呢?Unsafe.park的JNI实现,内部的核心方法就是如下代码,调用了Parker类的park方法。
class PlatformParker : public CHeapObj<mtInternal> { protected: enum { REL_INDEX = 0, ABS_INDEX = 1 }; int _cur_index; // 条件变量数组下标,which cond is in use: -1, 0, 1 pthread_mutex_t _mutex [1] ; //pthread互斥锁 pthread_cond_t _cond [2] ; // pthread条件变量数组,一个用于相对时间,一个用于绝对时间。 public: // TODO-FIXME: make dtor private ~PlatformParker() { guarantee (0, "invariant") ; } public: PlatformParker() { int status; status = pthread_cond_init (&_cond[REL_INDEX], os::Linux::condAttr()); assert_status(status == 0, status, "cond_init rel"); status = pthread_cond_init (&_cond[ABS_INDEX], NULL); assert_status(status == 0, status, "cond_init abs"); status = pthread_mutex_init (_mutex, NULL); assert_status(status == 0, status, "mutex_init"); _cur_index = -1; // mark as unused } };
class Parker : public os::PlatformParker { private: volatile int _counter ; //计数 Parker * FreeNext ; //指向下一个Parker JavaThread * AssociatedWith ; // 指向parker所属的线程。 public: Parker() : PlatformParker() { _counter = 0 ; //初始化为0 FreeNext = NULL ; AssociatedWith = NULL ; } protected: ~Parker() { ShouldNotReachHere(); } public: // For simplicity of interface with Java, all forms of park (indefinite, // relative, and absolute) are multiplexed into one call. void park(bool isAbsolute, jlong time); void unpark(); // Lifecycle operators static Parker * Allocate (JavaThread * t) ; static void Release (Parker * e) ; private: static Parker * volatile FreeList ; static volatile int ListLock ; };
创建线程的时候会创建一个Parker类实例,Parker.park代码如下:
void Parker::park(bool isAbsolute, jlong time) { //原子交换,如果_counter > 0,则将_counter置为0,直接返回,否则_counter为0 if (Atomic::xchg(0, &_counter) > 0) return; //获取当前线程 Thread* thread = Thread::current(); assert(thread->is_Java_thread(), "Must be JavaThread"); //下转型为java线程 JavaThread *jt = (JavaThread *)thread; //如果当前线程设置了中断标志,调用park则直接返回,所以如果在park之前调用了 //interrupt就会直接返回 if (Thread::is_interrupted(thread, false)) { return; } // 高精度绝对时间变量 timespec absTime; //如果time小于0,或者isAbsolute是true并且time等于0则直接返回 if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all return; } //如果time大于0,则根据是否是高精度定时计算定时时间 if (time > 0) { unpackTime(&absTime, isAbsolute, time); } //进入安全点避免死锁 ThreadBlockInVM tbivm(jt); //如果当前线程设置了中断标志,或者获取mutex互斥锁失败则直接返回 //由于Parker是每个线程都有的,所以_counter cond mutex都是每个线程都有的, //不是所有线程共享的所以加锁失败只有两种情况,第一unpark已经加锁这时只需要返回即可, //第二调用调用pthread_mutex_trylock出错。对于第一种情况就类似是unpark先调用的情况,所以 //直接返回。 if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) { return; } int status ; //如果_counter大于0,说明unpark已经调用完成了将_counter置为了1, //现在只需将_counter置0,解锁,返回 if (_counter > 0) { // no wait needed _counter = 0; status = pthread_mutex_unlock(_mutex); assert (status == 0, "invariant"); OrderAccess::fence(); return; } OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); jt->set_suspend_equivalent(); // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() assert(_cur_index == -1, "invariant"); //如果time等于0,说明是相对时间也就是isAbsolute是fasle(否则前面就直接返回了),则直接挂起 if (time == 0) { _cur_index = REL_INDEX; // arbitrary choice when not timed status = pthread_cond_wait (&_cond[_cur_index], _mutex) ; } else { //如果time非0 //判断isAbsolute是false还是true,false的话使用_cond[0],否则用_cond[1] _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX; //使用条件变量使得当前线程挂起。 status = os::Linux::safe_cond_timedwait (&_cond[_cur_index], _mutex, &absTime) ; //如果挂起失败则销毁当前的条件变量重新初始化。 if (status != 0 && WorkAroundNPTLTimedWaitHang) { pthread_cond_destroy (&_cond[_cur_index]) ; pthread_cond_init (&_cond[_cur_index], isAbsolute ? NULL : os::Linux::condAttr()); } } //如果pthread_cond_wait成功则以下代码都是线程被唤醒后执行的。 _cur_index = -1; assert_status(status == 0 || status == EINTR || status == ETIME || status == ETIMEDOUT, status, "cond_timedwait"); #ifdef ASSERT pthread_sigmask(SIG_SETMASK, &oldsigs, NULL); #endif //将_counter变量重新置为1 _counter = 0 ; //解锁 status = pthread_mutex_unlock(_mutex) ; assert_status(status == 0, status, "invariant") ; // 使用内存屏障使_counter对其它线程可见 OrderAccess::fence(); // 如果在park线程挂起的时候调用了stop或者suspend则还需要将线程挂起不能返回 if (jt->handle_special_suspend_equivalent_condition()) { jt->java_suspend_self(); } }
所以怎么理解park呢,简单理解就是使用互斥锁、条件变量,如果park之后会加上互斥锁,并添加到条件变量;可以被中断退出;步骤:
获取当前线程关联的 Parker 对象。
将计数器置为 0,同时检查计数器的原值是否为 1,如果是则放弃后续操作。
在互斥量上加锁。
在条件变量上阻塞,同时释放锁并等待被其他线程唤醒,当被唤醒后,将重新获取锁。
当线程恢复至运行状态后,将计数器的值再次置为 0。
释放锁。
参考如下图:
(2)unpark实现原理
void Parker::unpark() { int s, status ; //加互斥锁 status = pthread_mutex_lock(_mutex); assert (status == 0, "invariant") ; s = _counter; _counter = 1; //将_counter置1 //如果_counter是0则说明调用了park或者没调用(初始为counter0) //这也说明park和unpark调用没有先后顺序。 if (s < 1) { // 说明当前parker对应的线程挂起了,因为_cur_index初始是-1,并且等待条件变量的线程被唤醒 //后也会将_cur_index重置-1 if (_cur_index != -1) { //如果设置了WorkAroundNPTLTimedWaitHang先调用signal再调用unlock,否则相反 //这两个先后顺序都可以,在hotspot在Linux下默认使用这种方式 //即先调用signal再调用unlock if (WorkAroundNPTLTimedWaitHang) { status = pthread_cond_signal (&_cond[_cur_index]); assert (status == 0, "invariant"); status = pthread_mutex_unlock(_mutex); assert (status == 0, "invariant"); } else { status = pthread_mutex_unlock(_mutex); assert (status == 0, "invariant"); status = pthread_cond_signal (&_cond[_cur_index]); assert (status == 0, "invariant"); } } else { //如果_cur_index == -1说明线程没在等待条件变量,则直接解锁 pthread_mutex_unlock(_mutex); assert (status == 0, "invariant") ; } } else {//如果_counter == 1,说明线程调用了一次或多次unpark但是没调用park,则直接解锁 pthread_mutex_unlock(_mutex); assert (status == 0, "invariant") ; }
很简单,就是进行判断当前线程是否挂在条件变量上,如果是则signal唤醒,否则就不做任何操作。
获取目标线程关联的 Parker 对象(注意目标线程不是当前线程)
在互斥量上加锁
将计数器置为 1
唤醒在条件变量上等待着的线程
释放锁
2.3 volatile
put* :存储变量的引用到对象的指定的偏移量处,使用volatile的存储语义
get* :从对象的指定偏移量处获取变量的引用,使用volatile的加载语义
public native Object getObjectVolatile(Object var1, long var2); public native void putObjectVolatile(Object var1, long var2, Object var4); public native int getIntVolatile(Object var1, long var2); public native void putIntVolatile(Object var1, long var2, int var4); public native boolean getBooleanVolatile(Object var1, long var2); public native void putBooleanVolatile(Object var1, long var2, boolean var4); public native byte getByteVolatile(Object var1, long var2); public native void putByteVolatile(Object var1, long var2, byte var4); public native short getShortVolatile(Object var1, long var2); public native void putShortVolatile(Object var1, long var2, short var4); public native char getCharVolatile(Object var1, long var2); public native void putCharVolatile(Object var1, long var2, char var4); public native long getLongVolatile(Object var1, long var2); public native void putLongVolatile(Object var1, long var2, long var4); public native float getFloatVolatile(Object var1, long var2); public native void putFloatVolatile(Object var1, long var2, float var4); public native double getDoubleVolatile(Object var1, long var2); public native void putDoubleVolatile(Object var1, long var2, double var4); public native void putOrderedObject(Object var1, long var2, Object var4); public native void putOrderedInt(Object var1, long var2, int var4); public native void putOrderedLong(Object var1, long var2, long var4);
2.4 内存屏障
//内存屏障,禁止load操作重排序,即屏障前的load操作不能被重排序到屏障后,屏障后的load操作不能被重排序到屏障前 public native void loadFence(); //内存屏障,禁止store操作重排序,即屏障前的store操作不能被重排序到屏障后,屏障后的store操作不能被重排序到屏障前 public native void storeFence(); //内存屏障,禁止load、store操作重排序 public native void fullFence();
2.5 直接操作内存(非堆内存)
//(boolean、byte、char、short、int、long、float、double)都有以下get、put两个方法。 //获得给定地址上的int值 public native int getInt(long address); //设置给定地址上的int值 public native void putInt(long address, int x); //获得本地指针 public native long getAddress(long address); //存储本地指针到给定的内存地址 public native void putAddress(long address, long x); //分配内存 public native long allocateMemory(long bytes); //重新分配内存 public native long reallocateMemory(long address, long bytes); //初始化内存内容 public native void setMemory(Object o, long offset, long bytes, byte value); //初始化内存内容 public void setMemory(long address, long bytes, byte value) { setMemory(null, address, bytes, value); } //内存内容拷贝 public native void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes); //内存内容拷贝 public void copyMemory(long srcAddress, long destAddress, long bytes) { copyMemory(null, srcAddress, null, destAddress, bytes); } //释放内存 public native void freeMemory(long address);
2.6 系统相关
//返回指针的大小。返回值为4或8。 public native int addressSize(); /** The value of {@code addressSize()} */ public static final int ADDRESS_SIZE = theUnsafe.addressSize(); //内存页的大小。 public native int pageSize();
2.7 Class相关
//静态属性的偏移量,用于在对应的Class对象中读写静态属性
public native long staticFieldOffset(Field f);
public native Object staticFieldBase(Field f);
//判断是否需要初始化一个类
public native boolean shouldBeInitialized(Class<?> c);
//确保类被初始化
public native void ensureClassInitialized(Class<?> c);
//定义一个类,可用于动态创建类
public native Class<?> defineClass(String name, byte[] b, int off, int len,
ClassLoader loader,
ProtectionDomain protectionDomain);
//定义一个匿名类,可用于动态创建类
public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);
2.8 Object相关
//获得对象的字段偏移量 public native long objectFieldOffset(Field f); //获得给定对象地址偏移量的int值 public native int getInt(Object o, long offset); //设置给定对象地址偏移量的int值 public native void putInt(Object o, long offset, int x);
2.9 数组相关
/**
* Report the offset of the first element in the storage allocation of a
* given array class. If {@link #arrayIndexScale} returns a non-zero value
* for the same class, you may use that scale factor, together with this
* base offset, to form new offsets to access elements of arrays of the
* given class.
*
* @see #getInt(Object, long)
* @see #putInt(Object, long, int)
*/
//返回数组中第一个元素的偏移地址
public native int arrayBaseOffset(Class<?> arrayClass);
//boolean、byte、short、char、int、long、float、double,及对象类型均有以下方法
/** The value of {@code arrayBaseOffset(boolean[].class)} */
public static final int ARRAY_BOOLEAN_BASE_OFFSET
= theUnsafe.arrayBaseOffset(boolean[].class);
/**
* Report the scale factor for addressing elements in the storage
* allocation of a given array class. However, arrays of "narrow" types
* will generally not work properly with accessors like {@link
* #getByte(Object, int)}, so the scale factor for such classes is reported
* as zero.
*
* @see #arrayBaseOffset
* @see #getInt(Object, long)
* @see #putInt(Object, long, int)
*/
//返回数组中每一个元素占用的大小
public native int arrayIndexScale(Class<?> arrayClass);
//boolean、byte、short、char、int、long、float、double,及对象类型均有以下方法
/** The value of {@code arrayIndexScale(boolean[].class)} */
public static final int ARRAY_BOOLEAN_INDEX_SCALE
= theUnsafe.arrayIndexScale(boolean[].class);
3 原子类
原子类主要就是CAS + Volitile的组合实现的,以AtomicInteger为例:
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(); private static final long valueOffset; static { try { 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; } /** * Creates a new AtomicInteger with initial value {@code 0}. */ public AtomicInteger() { } /** * Atomically sets to the given value and returns the old value. * * @param newValue the new value * @return the previous value */ public final int getAndSet(int newValue) { return unsafe.getAndSetInt(this, valueOffset, newValue); } /** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * * @param expect the expected value * @param update the new value * @return {@code true} if successful. False return indicates that * the actual value was not equal to the expected value. */ public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** * Atomically increments by one the current value. * * @return the previous value */ public final int getAndIncrement() { return unsafe.getAndAddInt(this, valueOffset, 1); } /** * Atomically adds the given value to the current value. * * @param delta the value to add * @return the previous value */ public final int getAndAdd(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta); } /** * Atomically increments by one the current value. * * @return the updated value */ public final int incrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, 1) + 1; } /** * Atomically decrements by one the current value. * * @return the updated value */ public final int decrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, -1) - 1; } /** * Atomically adds the given value to the current value. * * @param delta the value to add * @return the updated value */ public final int addAndGet(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta) + delta; } }
volatile关键字保证了value的可见性,CAS保证了原子性,从而整体保证了线程安全的类;还有一些AtomicLong、AtomicBoolean实现方式基本一样,大致原理一致,底层使用的unsafe.getAndAddInt(this, valueOffset, delta)
public final int getAndAddInt(Object var1, long var2, int var4) { int var5; do { var5 = this.getIntVolatile(var1, var2); } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); return var5; }
使用了getIntVolatile就是使用了volatile load语义读取数据,然后使用cas进行赋值,保证线程安全。
参考:
[1] https://www.pdai.tech/md/java/thread/java-thread-x-juc-AtomicInteger.html
[2] https://blog.csdn.net/saintyyu/article/details/107426428