atomicInteger, atomicLong,atomicBoolean
1.代码countdownlatch
- package com.ljf.thread.atomic;
-
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.atomic.AtomicInteger;
-
- class MyNumber
- {
- AtomicInteger atomicInteger = new AtomicInteger();
-
- public void addPlusPlus()
- {
- atomicInteger.getAndIncrement();
- }
- }
- /**
- * @ClassName: AtomicDemo
- * @Description: TODO
- * @Author: admin
- * @Date: 2023/11/06 22:24:14
- * @Version: V1.0
- **/
- public class AtomicDemo {
-
- public static void main(String[] args) throws InterruptedException {
- MyNumber myNumber = new MyNumber();
- CountDownLatch ct=new CountDownLatch(10);
- for(int k=1;k<=10;k++){
- new Thread(new Runnable() {
- @Override
- public void run() {
- for(int k=0;k<30;k++){
- myNumber.addPlusPlus();
- }
- ct.countDown();
- }
- },String.valueOf(k)).start();
- }
- ct.await();
- System.out.println("");
- System.out.println(Thread.currentThread().getName()+"\t"+"result: "+myNumber.atomicInteger.get());
- }
- }
结果:
1.AtomicIntegerArray 2.AtomicLongArray 3. AtomicReferenceArray
1.AtomicReference
2.AtomicStampedReference 携带版本号的引用类,解决修改过几次
3.AtomicMarkableReference 原子更新带有标记位 解决是否修改过 状态戳 true/false
1.AtomicIntegerFieldUpdater 原子更新对象中int类型字段的值
2.AtomicLongFieldUpdater 原子更新对象中Long类型字段的值。
3.AtomicReferenceFieldUpdater 原子更新引用数据类型字段的值
目的:以一种线程安全的方式操作非线程安全的对象内的某些字段。更新的对象属性必须使用public volatile修饰符。因为对象的属性修改类型原子类都是抽象类,所有每次使用都必须使用静态方法newUpdate()方法创建一个更新器,并且需要设置想要更新的类和属性。
1.原始锁方式
- public class AtomicDemo {
- public static void main(String[] args) throws InterruptedException {
- CountDownLatch countDownLatch=new CountDownLatch(10);
- PayInfo payInfo =new PayInfo();
- for(int k=0;k<10;k++){
- new Thread(()->{
- try {
- for (int m = 0; m < 1000; m++) {
- payInfo.add();
- //payInfo.transferMoney(payInfo);
- }
- }
- catch (Exception e){
- e.printStackTrace();
- }
- finally {
- countDownLatch.countDown();
- }
- }).start();
- }
- countDownLatch.await();
- System.out.println(Thread.currentThread().getName()+"\t"+payInfo.money);
- }
- }
资源类
- public class PayInfo {
- String bankName = "icbc";
- int money = 0;
- // public volatile int money = 0;
- public synchronized void add(){
- money++;
- }
- // AtomicIntegerFieldUpdater<PayInfo> atomicIntegerFieldUpdater=AtomicIntegerFieldUpdater.newUpdater(PayInfo.class,"money");
- // public void transferMoney(PayInfo payInfo){
- // atomicIntegerFieldUpdater.getAndIncrement(payInfo);
- // }
- }
3.结果
1.资源类
- public class PayInfo {
- String bankName = "icbc";
- // int money = 0;
- public volatile int money = 0;
- public synchronized void add(){
- money++;
- }
- AtomicIntegerFieldUpdater<PayInfo> atomicIntegerFieldUpdater=AtomicIntegerFieldUpdater.newUpdater(PayInfo.class,"money");
- public void transferMoney(PayInfo payInfo){
- atomicIntegerFieldUpdater.getAndIncrement(payInfo);
- }
- }
2.代码
- public class AtomicDemo {
- public static void main(String[] args) throws InterruptedException {
- CountDownLatch countDownLatch=new CountDownLatch(10);
- PayInfo payInfo =new PayInfo();
- for(int k=0;k<10;k++){
- new Thread(()->{
- try {
- for (int m = 0; m < 1000; m++) {
- //payInfo.add();
- payInfo.transferMoney(payInfo);
- }
- }
- catch (Exception e){
- e.printStackTrace();
- }
- finally {
- countDownLatch.countDown();
- }
- }).start();
- }
- countDownLatch.await();
- System.out.println(Thread.currentThread().getName()+"\t"+payInfo.money);
- }
- }
3.结果
LongAdder 只能用来计算加法,且从零开始计算。
LongAccumulator提供了自定义的函数操作。
- public class LongAdderDemo {
- public static void main(String[] args) {
- LongAdder longAdder=new LongAdder();
- longAdder.increment();
- longAdder.increment();
- System.out.println("result:"+longAdder.toString());
- LongAccumulator longAccumulator = new LongAccumulator(new LongBinaryOperator()
- {
- @Override
- public long applyAsLong(long left, long right)
- {
- System.out.println("left:"+left+" right:"+right);
- return left + right;
- }
- },9);
-
- longAccumulator.accumulate(1);//1
- longAccumulator.accumulate(3);//4
-
- System.out.println(longAccumulator.get());
- }
- }
结果:
1.代码
- package com.ljf.thread.atomic;
-
- import java.util.concurrent.atomic.AtomicLong;
- import java.util.concurrent.atomic.LongAccumulator;
- import java.util.concurrent.atomic.LongAdder;
-
- /**
- * @ClassName: Clik
- * @Description: TODO
- * @Author: admin
- * @Date: 2023/12/24 10:51:07
- * @Version: V1.0
- **/
- public class Clik {
- int number = 0;
- public synchronized void clickBySynchronized()
- {
- number++;
- }
-
- AtomicLong atomicLong = new AtomicLong(0);
- public void clickByAtomicLong()
- {
- atomicLong.getAndIncrement();
- }
-
- LongAdder longAdder = new LongAdder();
- public void clickByLongAdder()
- {
- longAdder.increment();
- }
-
- LongAccumulator longAccumulator = new LongAccumulator((x, y) -> x + y,0);
- public void clickByLongAccumulator()
- {
- longAccumulator.accumulate(1);
- }
- }
2.调用类
- package com.ljf.thread.atomic;
-
- import java.util.concurrent.CountDownLatch;
-
- /**
- * @ClassName: AccumulatorCompareDemo
- * @Description: TODO
- * @Author: admin
- * @Date: 2023/12/24 10:51:51
- * @Version: V1.0
- **/
- public class AccumulatorCompareDemo {
- public static final int _1W = 10000;
- public static final int threadNumber = 50;
- public static void main(String[] args) throws InterruptedException {
- long startTime;
- long endTime;
- Clik clik=new Clik();
- CountDownLatch countDownLatch1 = new CountDownLatch(threadNumber);
- CountDownLatch countDownLatch2 = new CountDownLatch(threadNumber);
- CountDownLatch countDownLatch3 = new CountDownLatch(threadNumber);
- CountDownLatch countDownLatch4 = new CountDownLatch(threadNumber);
-
- startTime = System.currentTimeMillis();
- for (int i = 1; i <=threadNumber; i++) {
- new Thread(() -> {
- try {
- for (int j = 1; j <=100 * _1W; j++) {
- clik.clickBySynchronized();
- }
- } finally {
- countDownLatch1.countDown();
- }
- },String.valueOf(i)).start();
- }
- countDownLatch1.await();
- endTime = System.currentTimeMillis();
- System.out.println("----costTime: "+(endTime - startTime) +" 毫秒"+"\t clickBySynchronized: "+clik.number);
- //
- startTime = System.currentTimeMillis();
- for (int i = 1; i <=threadNumber; i++) {
- new Thread(() -> {
- try {
- for (int j = 1; j <=100 * _1W; j++) {
- clik.clickByAtomicLong();
- }
- } finally {
- countDownLatch2.countDown();
- }
- },String.valueOf(i)).start();
- }
- countDownLatch2.await();
- endTime = System.currentTimeMillis();
- System.out.println("----costTime: "+(endTime - startTime) +" 毫秒"+"\t clickByAtomicLong: "+clik.number);
- //
- //
- //
- startTime = System.currentTimeMillis();
- for (int i = 1; i <=threadNumber; i++) {
- new Thread(() -> {
- try {
- for (int j = 1; j <=100 * _1W; j++) {
- clik.clickByLongAdder();
- }
- } finally {
- countDownLatch3.countDown();
- }
- },String.valueOf(i)).start();
- }
- countDownLatch3.await();
- endTime = System.currentTimeMillis();
- System.out.println("----costTime: "+(endTime - startTime) +" 毫秒"+"\t clickByLongAdder: "+clik.number);
-
- //
- startTime = System.currentTimeMillis();
- for (int i = 1; i <=threadNumber; i++) {
- new Thread(() -> {
- try {
- for (int j = 1; j <=100 * _1W; j++) {
- clik.clickByLongAccumulator();
- }
- } finally {
- countDownLatch4.countDown();
- }
- },String.valueOf(i)).start();
- }
- countDownLatch4.await();
- endTime = System.currentTimeMillis();
- System.out.println("----costTime: "+(endTime - startTime) +" 毫秒"+"\t clickByLongAccumulator: "+clik.number);
-
- }
-
-
-
- }