AtomicInteger_用法_写给自己参考的
AtomicInteger
getAndIncrement()
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger index=new AtomicInteger(0);
System.out.println(index.getAndIncrement());//0 //先返回值然后再执行+1操作
System.out.println(index.getAndIncrement());//1
index.set(0);
System.out.println(index.get());//0
index.incrementAndGet(); //先执行增加然后 再返回
incrementAndGet
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger index=new AtomicInteger(0);
System.out.println(index.getAndIncrement());//0 //先返回值然后再执行+1操作
System.out.println(index.getAndIncrement());//1
index.set(0);
System.out.println(index.get());//0
index.incrementAndGet(); //先执行增加然后 再返回
getAndUpdate–负载使用场景
pubLic String getDataSource(final String name,final String masterDataSourceName,
finaL List<String>slaveDataSourceNames) {
List<String> toReadDatasourceNames=filter(slaveDataSourceNames,masterDataSourceName);
if(toReadDatasourceNames.size()==0) {
//没有可用从库,则直接到主库
return masterDataSourceName;
} else{
AtomicInteger count=COUNTS.containsKey(name) ? COUNTS.get(name) :
new AtomicInteger(0) ;
COUNTS.putIfAbsent(name,count) ;
count.getAndUpdate(x->{
if(x>=MAX_INT) {
x=0;
} eLse{
X++;
}
return x;
};
return toReadDatasourceNames.get(Math.abs(count.get()) % toReadDatasourceNames.size());
}
}
LongAddr 高并发
LongAddr longAddr = new LongAddr();
longAddr.increment();
原理解释如下
AtomicBoolean 如何让某段代码执行一次绝对不重复
如果是false的时候,则变成true 这个类在实际还是比较常用的
AtomicReference
public static AtomicReference<Integer> count = new AtomicReference<>(0);
public static void main(String[] args) {
count.compareAndSet(0, 2);//2
count.compareAndSet(0, 1);
count.compareAndSet(1, 3);
count.compareAndSet(2, 4);//4
count.compareAndSet(3, 5);
System.out.println(count.get());
}
AtomicIntegerFieldUpdater或者AtomicLongFieldUpdater
compareAndGet
更改类中的一个字段 并且是并发安全的 有两个条件
- 该字段不能是static来修饰
2.该字段必须使用 voliate 关键字来修饰 - 该字段必须 是 public 来修改
addAndGet
1.定义
AtomicLongFieldUpdater<DruidAbstractDataSource> connectionIdSeedUpdater=AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class,"connectionIdSeed");
AtomicLongFieldUpdater<DruidAbstractDataSource> statementIdSeedUpdater=AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class,"statementIdSeed");
AtomicLongFieldUpdater<DruidAbstractDataSource> resultSetIdSeedUpdater=AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class,"resultSetIdSeed");
AtomicLongFieldUpdater<DruidAbstractDataSource> transactionIdSeedUpdater=AtomicLongFieldUpdater.newUpdater.(DruidAbstractDataSource.class,"transactionIdSeed");
AtomicLongFieldUpdater<DruidAbstractDataSource> metaDataIdSeedUpdater=AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class,"metaDataIdSeed");
----------------------------------------------------
2.DruidAbstractDataSource里的connectionIdSeed属性涨啥样儿
protected volatile Long statementIdSeed=10000L
protected volatile Long connectionIdSeed=20000L
--------------------------------------------------
3.addAndGet放入值
long delta=(this.id-1) *100000;
this.connection IdSeedUpdater.addAndGet(this,delta);
this.statementIdSeedUpdater.addAndGet(this,delta);
this.resultSetIdSeedUpdater.addAndGet(this,delta);
this.transactionIdSeedUpdater.addAndGet(this,delta);
AtomicStampReference
解决ABA问题
- 其它线程将原始A改成B,又改回A
而本线程发现是A 就进行操作
因为这个时候被其实A变过这与CAS设计思想是不符合的
ABA 的解决思路是每次变量更新 把变量的版本号加1
核心的方法
compareAndSet()
AtomicLongArray
一个数组,可以实现对数组元素进行操作 操作的时候要求传入索引值