欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CAS自定义线程安全的计数器

程序员文章站 2023-12-28 14:24:58
...
写了一个线程安全的计数器(用锁显得多没有水平),非阻塞的,通过自旋的CAS操作达到数据一致性的效果

[b]开始[/b]先说下[b]原子变量[/b]和[b]锁[/b]的[b]比较[/b]
[size=medium][b]锁在高竞争的时候效率会高于原子变量,但是在中低竞争的时候原子变量的性能会好一些[/b][/size]
[b]类比[/b]:(交通拥堵时,交通信号灯能够实现更高的吞吐量,而在低拥堵时,环岛能实现更高的吞吐量)

[b]知识点:[/b]
[size=medium]UnSafe的CAS操作[/size]
    unsafe.compareAndSwapInt(this, valueOffset, oleVal, newVal)

[size=medium]UnSafe的实例化[/size]
[b] 1.JDK适用UnSafe是通过下面的代码获取的[/b]
private static final Unsafe unsafe = Unsafe.getUnsafe();

但是自定义使用UnSafe无法获取,会抛异常,原因如下代码。
        public static Unsafe getUnsafe() {
Class var0 = Reflection.getCallerClass();
if(var0.getClassLoader() != null) {
throw new SecurityException("Unsafe");
} else {
return theUnsafe;
}
}

[b] 2.构造函数私有化,无法new[/b]
[b] 3.么办法,只能反射了[/b]
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(Unsafe.class);
} catch (Exception e) {
e.printStackTrace();
}


[size=medium]CyclicBarrier(循环栅栏)[/size]
[b] 1.提供了两种初始化的构造方法[/b]
parties:到达栅栏的线程数目=parties,会释放栅栏
包含Runnable的构造方法,Runnable会在所有线程通过栅栏的时候触发
    public CyclicBarrier(int parties)
public CyclicBarrier(int parties, Runnable barrierAction)

[b]2.CyclicBarrier可以循环使用[/b]
[size=medium][b]

下面是代码,分两个部分,计数器类和测试类[/b][/size]

import sun.misc.Unsafe;
import java.io.Serializable;
import java.lang.reflect.Field;

public class CasCounter implements Serializable {
private static Unsafe unsafe;
private static long valueOffset;
static {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(Unsafe.class);
} catch (Exception e) {
e.printStackTrace();
}
try {
valueOffset = unsafe.objectFieldOffset(CasCounter.class.getDeclaredField("value"));
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}

private volatile int value;
public CasCounter() {value = 0;}
public CasCounter(int initValue) {this.value = initValue;}

public int getValue() {
return value;
}

public int increment(int incrNum) {//线程安全
while (true) {
int oleVal = value;
int newVal = oleVal + incrNum;
if (unsafe.compareAndSwapInt(this, valueOffset, oleVal, newVal)) {
return newVal;
}
}
}

public int incrementNo(int incrNum) {//非线程安全
value += incrNum;
return value;
}
}



import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Test {

public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
final CasCounter casCounter = new CasCounter();

final CyclicBarrier cb = new CyclicBarrier(1001, new Runnable() {
public void run() {
System.out.printf("finalVal : %s \n", casCounter.getValue());
}
});

for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
public void run() {
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}

for (int j = 0; j < 10; j++) {
int curr = casCounter.increment(1);
System.out.print("");//这行代码会增加冲突概率
}

try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}).start();
}
cb.await();
cb.await();
}
}


结果是1000*10=10000应该是正确的,在进行了上百次的测试的结果都是10000,程序应该没问题

finalVal : 0
finalVal : 10000
相关标签: UnSafe CAS java

上一篇:

下一篇: