package com.dym.atomic;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class T1_AtomicInteger {
public static int total = 0;
static AtomicInteger atomic = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(10);
for(int i=0;i<10;i++){
new Thread(()->{
for(int j=0;j<10000;j++){
total++;
atomic.getAndIncrement();
}
countDownLatch.countDown();
}).start();
}
countDownLatch.await();
System.out.println("total=======>>"+total);
System.out.println("AtomicInteger======>>"+atomic.get());
}
}
AtomicInteger 的用法----》》既能保证线程安全又能保证效率
最新推荐文章于 2022-04-07 08:00:00 发布