LockSupport线程阻塞/唤醒工具类
目录
LockSupport原理
LockSupport类,是个工具类。内部的实现是调用sun.misc.Unsafe(rt.jar包中)这个native本地类中的方法实现的,Unsafe本地类是用C++写的。可以说concurrent包中的绝大部分类都是最终调用到了Unsafe类中的方法,下面附上Unsafe类的部分源码及其说明:
package sun.misc;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import sun.reflect.CallerSensitive;
import sun.reflect.Reflection;
/**
* volatile, CAS操作, 自旋结构 组合在一起构成了java.util.concurrent包
*
* volatile 修饰的变量如果是对象或数组,其含义是对象或数组的地址具有可见性,但是数组或对象内部的成员改变不具备可见性
*
* CAS操作; lock前缀指令,执行该cas指令时会锁住缓冲行
*
*自旋结构; for(;;){ 读内存值; if(cas操作){return;} }
*/
//Unsafe类在%JAVA_HOME%/jre/lib/rt.jar包中
public final class Unsafe
{
private static final Unsafe theUnsafe = new Unsafe();
public static final int INVALID_FIELD_OFFSET = -1;
/******************LockSupport类调用的****************************/
public native void unpark(Object paramObject);
public native void park(boolean paramBoolean, long paramLong);
/******************AtomicReference<T>类调用的****************************/
/**
* 自旋结构:for(;;){ 读内存值; if(cas操作compareAndSwapObject){return;} }
* 参数 paramObject1 cpu旧值
* 参数 paramLong 偏移量
* 参数 paramObject2 cpu新值
*/
public final Object getAndSetObject(Object paramObject1, long paramLong, Object paramObject2)
{
Object localObject;
do
{
localObject = getObjectVolatile(paramObject1, paramLong);
} while (!compareAndSwapObject(paramObject1, paramLong, localObject, paramObject2));
return localObject;
}
//原子操作:比较并转换:cpu内旧值paramObject1, 偏移量paramLong, 内存当前值paramObject2, cpu内新值paramObject3
public final native boolean compareAndSwapObject(Object paramObject1, long paramLong, Object paramObject2, Object paramObject3);
//原子操作:读取可见性内存值
public native Object getObjectVolatile(Object paramObject, long paramLong);
/******************AtomicInteger类调用的****************************/
/**
* 自旋结构:for(;;){ 读内存值; if(cas操作compareAndSwapInt){return;} }
* 参数 paramObject1 cpu旧值
* 参数 paramLong 偏移量
* 参数 paramInt 累加
*/
public final int getAndAddInt(Object paramObject, long paramLong, int paramInt)
{
int i;
do
{
i = getIntVolatile(paramObject, paramLong);
} while (!compareAndSwapInt(paramObject, paramLong, i, i + paramInt));
return i;
}
//原子操作:比较并转换:cpu内旧值paramObject1, 偏移量paramLong, 内存当前值paramObject2, cpu内新值paramObject3
public final native boolean compareAndSwapInt(Object paramObject, long paramLong, int paramInt1, int paramInt2);
public native int getIntVolatile(Object paramObject, long paramLong);
/**
* 自旋结构:for(;;){ 读内存值; if(cas操作compareAndSwapInt){return;} }
* 参数 paramObject cpu旧值
* 参数 paramLong 偏移量
* 参数 paramInt cpu新值
*/
public final int getAndSetInt(Object paramObject, long paramLong, int paramInt)
{
int i;
do
{
i = getIntVolatile(paramObject, paramLong);
} while (!compareAndSwapInt(paramObject, paramLong, i, paramInt));
return i;
}
public void setMemory(long paramLong1, long paramLong2, byte paramByte)
{
setMemory(null, paramLong1, paramLong2, paramByte);
}
public void copyMemory(long paramLong1, long paramLong2, long paramLong3)
{
copyMemory(null, paramLong1, null, paramLong2, paramLong3);
}
public native void setMemory(Object paramObject, long paramLong1, long paramLong2, byte paramByte);
public native void copyMemory(Object paramObject1, long paramLong1, Object paramObject2, long paramLong2, long paramLong3);
}
- park()方法的本质可以理解为:雷同Object对象的wait()方法,作用是将当前线程阻塞
- unpark()方法的本质可以理解为:雷同Object对象的notify()方法,作用是将线程唤醒
LockSupport示例