原子性操作,在多線程下 該變量的操作是原子性的,不需要添加:synchronized
例子
啟用100個線程,每個線程做+1的操作,最終的結(jié)果應(yīng)該會得到 100才對
public static void main(String args[]) throws InterruptedException {
//使用線程池
ExecutorService service = Executors.newCachedThreadPool();
TestCount testCount=new TestCount();
long time=System.currentTimeMillis();
//100個線程對count+1
for(int i=1;i<=100;i++){
service.execute(()->testCount.increase());
}
// 等待上述的線程執(zhí)行完
service.shutdown();
service.awaitTermination(1, TimeUnit.DAYS);
System.out.println("計算結(jié)果:"+testCount.getCount());
}
//類
public static final class TestCount{
private Integer count=0;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public void increase(){
count++;
}
}
最終的記結(jié)果會出現(xiàn) 97 96 .等的情況,就是說count 這個共享變量是線程不安全的呀,線程都共享這個變量

使用 AtomicInteger 修改count 變量
public static final class TestCount{
private AtomicInteger count = new AtomicInteger(0);
public Integer getCount() {
return count.get();
}
public void increase(){
count.incrementAndGet();
}
}
不管執(zhí)行多少次都是100 結(jié)果正確

Atomic 的類

基本類型:
AtomicBoolean:布爾型
AtomicInteger:整型
AtomicLong:長整型
數(shù)組:
AtomicIntegerArray:數(shù)組里的整型
AtomicLongArray:數(shù)組里的長整型
AtomicReferenceArray:數(shù)組里的引用類型
引用類型:
AtomicReference:引用類型
AtomicStampedReference:帶有版本號的引用類型
AtomicMarkableReference:帶有標(biāo)記位的引用類型
對象的屬性:
AtomicIntegerFieldUpdater:對象的屬性是整型
AtomicLongFieldUpdater:對象的屬性是長整型
AtomicReferenceFieldUpdater:對象的屬性是引用類型
JDK8新增DoubleAccumulator、LongAccumulator、DoubleAdder、LongAdder
是對AtomicLong等類的改進(jìn)。比如LongAccumulator與LongAdder在高并發(fā)環(huán)境下比AtomicLong更高效。
AtomicReferenceFieldUpdater、AtomicIntegerFieldUpdater和AtomicLongFieldUpdater是基于反射的實用工具,可以提供對關(guān)聯(lián)字段類型的訪問。例如AtomicIntegerFieldUpdater可以對指定類的指定volatile int字段進(jìn)行原子更新。
原子類可以替換鎖嗎?
原子類不是鎖的常規(guī)替換方法。僅當(dāng)對象的重要更新限定于單個變量時才應(yīng)用它。
原子類和java.lang.Integer等類的區(qū)別
原子類不提供諸如hashCode和compareTo之類的方法。因為原子變量是可變的。
:LongAdder中會維護一組(一個或多個)變量,這些變量加起來就是要以原子方式更新的long型變量。當(dāng)更新方法add(long)在線程間競爭時,該組變量可以動態(tài)增長以減緩競爭。方法sum()返回當(dāng)前在維持總和的變量上的總和。與AtomicLong相比,LongAdder更多地用于收集統(tǒng)計數(shù)據(jù),而不是細(xì)粒度的同步控制。在低并發(fā)環(huán)境下,兩者性能很相似。但在高并發(fā)環(huán)境下,LongAdder有著明顯更高的吞吐量,但是有著更高的空間復(fù)雜度。
import java.util.concurrent.atomic.AtomicLong;
class Counter {
private static AtomicLong counter = new AtomicLong(0);
public static long addOne() {
return counter.incrementAndGet();
}
}