如果内存位置的值与预期原值相匹配,那么处理器会自动将该位置值更新为新值。否则,处理器不做任何操作。
Java并发包(java.util.concurrent)中大量使用了CAS操作,涉及到并发的地方都调用了sun.misc.Unsafe类方法进行CAS操作。
Unsafe的APIS:
1.取得Unsafe实例。
private static final sun.misc.Unsafe UNSAFE;
try {
UNSAFE = sun.misc.Unsafe.getUnsafe();
} catch (Exception e) {
throw new Error(e);
}
2.native methods。
// **********************************************************************offset**************************************************************************
2.1 返回指定field的内存偏移地址,该值用于访问该field的特定方式,该值唯一的。
/***
* Returns the memory address offset of the given static field.
* The offset is merely used as a means to access a particular field
* in the other methods of this class. The value is unique to the given
* field and the same value should be returned on each subsequent call.
*
* @param field the field whose offset should be returned.
* @return the offset of the given field.
*/
public native long objectFieldOffset(Field field);
2.2 获取给定数组的第一个元素的偏移地址。
/***
* Returns the offset of the first element for a given array class.
* To access elements of the array class, this value may be used along with
* with that returned by
* <a href="#arrayIndexScale"><code>arrayIndexScale</code></a>,
* if non-zero.
* @param arrayClass the class for which the first element's address should
* be obtained.
* @return the offset of the first element of the array class.
* @see arrayIndexScale(Class)
*/
public native int arrayBaseOffset(Class arrayClass);
2.3 获取用户给定数组寻址的换算因子。
/***
* Returns the scale factor used for addressing elements of the supplied
* array class. Where a suitable scale factor can not be returned (e.g.
* for primitive types), zero should be returned. The returned value
* can be used with
* <a href="#arrayBaseOffset"><code>arrayBaseOffset</code></a>
* to access elements of the class.
*
* @param arrayClass the class whose scale factor should be returned.
* @return the scale factor, or zero if not supported for this array class.
*/
public native int arrayIndexScale(Class arrayClass);
计算数组元素的偏移地址方法:
Class tc = Object[].class;
final long BASE = UNSAFE.arrayBaseOffset(tc);
final int SHIFT = 31 - Integer.numberOfLeadingZeros(ts);
long u = (h << SHIFT) + BASE;
// **********************************************************************int**************************************************************************
2.4 CAS给定Object的int整型变量的值。
/***
* Compares the value of the integer field at the specified offset
* in the supplied object with the given expected value, and updates
* it if they match. The operation of this method should be atomic,
* thus providing an uninterruptible way of updating an integer field.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the integer field within <code>obj</code>.
* @param expect the expected value of the field.
* @param update the new value of the field if it equals <code>expect</code>.
* @return true if the field was changed.
*/
public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);
2.5 设置给定Object的int整型变量的值。该设置,其他线程不能立刻可见。
/***
* Sets the value of the integer field at the specified offset in the
* supplied object to the given value. This is an ordered or lazy
* version of <code>putIntVolatile(Object,long,int)</code>, which
* doesn't guarantee the immediate visibility of the change to other
* threads. It is only really useful where the integer field is
* <code>volatile</code>, and is thus expected to change unexpectedly.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the integer field within <code>obj</code>.
* @param value the new value of the field.
* @see #putIntVolatile(Object,long,int)
*/
public native void putOrderedInt(Object obj, long offset, int value);
2.6 设置给定Object的int整型变量的值。该设置,其他线程立刻可见。
/***
* Sets the value of the integer field at the specified offset in the
* supplied object to the given value, with volatile store semantics.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the integer field within <code>obj</code>.
* @param value the new value of the field.
*/
public native void putIntVolatile(Object obj, long offset, int value);
2.7 获取给定Object的int整型变量的值,volatile加载。
/***
* Retrieves the value of the integer field at the specified offset in the
* supplied object with volatile load semantics.
*
* @param obj the object containing the field to read.
* @param offset the offset of the integer field within <code>obj</code>.
*/
public native int getIntVolatile(Object obj, long offset);
// **********************************************************************long**************************************************************************
2.8 CAS给定Object的long长整型变量的值。
/***
* Compares the value of the long field at the specified offset
* in the supplied object with the given expected value, and updates
* it if they match. The operation of this method should be atomic,
* thus providing an uninterruptible way of updating a long field.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the long field within <code>obj</code>.
* @param expect the expected value of the field.
* @param update the new value of the field if it equals <code>expect</code>.
* @return true if the field was changed.
*/
public native boolean compareAndSwapLong(Object obj, long offset, long expect, long update);
2.9 设置给定Object的long长整型变量的值。该设置,其他线程不能立刻可见。
/***
* Sets the value of the long field at the specified offset in the
* supplied object to the given value. This is an ordered or lazy
* version of <code>putLongVolatile(Object,long,long)</code>, which
* doesn't guarantee the immediate visibility of the change to other
* threads. It is only really useful where the long field is
* <code>volatile</code>, and is thus expected to change unexpectedly.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the long field within <code>obj</code>.
* @param value the new value of the field.
* @see #putLongVolatile(Object,long,long)
*/
public native void putOrderedLong(Object obj, long offset, long value);
2.10 设置给定Object的long长整型变量的值。该设置,其他线程立刻可见。
/***
* Sets the value of the long field at the specified offset in the
* supplied object to the given value, with volatile store semantics.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the long field within <code>obj</code>.
* @param value the new value of the field.
* @see #putLong(Object,long,long)
*/
public native void putLongVolatile(Object obj, long offset, long value);
2.11 获取给定Object的long整型变量的值,volatile加载。
/***
* Retrieves the value of the long field at the specified offset in the
* supplied object with volatile load semantics.
*
* @param obj the object containing the field to read.
* @param offset the offset of the long field within <code>obj</code>.
* @see #getLong(Object,long)
*/
public native long getLongVolatile(Object obj, long offset);
// **********************************************************************Object**************************************************************************
2.12 CAS给定Object的对象类型变量的值。
/***
* Compares the value of the object field at the specified offset
* in the supplied object with the given expected value, and updates
* it if they match. The operation of this method should be atomic,
* thus providing an uninterruptible way of updating an object field.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the object field within <code>obj</code>.
* @param expect the expected value of the field.
* @param update the new value of the field if it equals <code>expect</code>.
* @return true if the field was changed.
*/
public native boolean compareAndSwapObject(Object obj, long offset, Object expect, Object update);
2.13 设置给定Object的对象类型变量的值。该设置,其他线程不能立刻可见。
/***
* Sets the value of the object field at the specified offset in the
* supplied object to the given value. This is an ordered or lazy
* version of <code>putObjectVolatile(Object,long,Object)</code>, which
* doesn't guarantee the immediate visibility of the change to other
* threads. It is only really useful where the object field is
* <code>volatile</code>, and is thus expected to change unexpectedly.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the object field within <code>obj</code>.
* @param value the new value of the field.
*/
public native void putOrderedObject(Object obj, long offset, Object value);
2.14 设置给定Object的对象类型变量的值。该设置,其他线程立刻可见。
/***
* Sets the value of the object field at the specified offset in the
* supplied object to the given value, with volatile store semantics.
*
* @param obj the object containing the field to modify.
* @param offset the offset of the object field within <code>obj</code>.
* @param value the new value of the field.
* @see #putObject(Object,long,Object)
*/
public native void putObjectVolatile(Object obj, long offset, Object value);
2.15 获取给定Object的对象类型变量的值,volatile加载。
/***
* Retrieves the value of the object field at the specified offset in the
* supplied object with volatile load semantics.
*
* @param obj the object containing the field to read.
* @param offset the offset of the object field within <code>obj</code>.
*/
public native Object getObjectVolatile(Object obj, long offset);
// **********************************************************************block-thread**************************************************************************
2.16 阻塞线程。
/***
* Blocks the thread until a matching
* <a href="#unpark"><code>unpark</code></a> occurs, the thread is
* interrupted or the optional timeout expires. If an <code>unpark</code>
* call has already occurred, this also counts. A timeout value of zero
* is defined as no timeout. When <code>isAbsolute</code> is
* <code>true</code>, the timeout is in milliseconds relative to the
* epoch. Otherwise, the value is the number of nanoseconds which must
* occur before timeout. This call may also return spuriously (i.e.
* for no apparent reason).
*
* @param isAbsolute true if the timeout is specified in milliseconds from
* the epoch.
* @param time either the number of nanoseconds to wait, or a time in
* milliseconds from the epoch to wait for.
*/
public native void park(boolean isAbsolute, long time);
2.17 释放park的阻塞。
/***
* Releases the block on a thread created by
* <a href="#park"><code>park</code></a>. This method can also be used
* to terminate a blockage caused by a prior call to <code>park</code>.
* This operation is unsafe, as the thread must be guaranteed to be
* live. This is true of Java, but not native code.
* @param thread the thread to unblock.
*/
public native void unpark(Thread thread);