JAVA原子类

原子类

在java.util.concurrent.atomic包下定义了一些对“变量”操作的“原子类”:

1).java.util.concurrent.atomic.AtomicInteger:对int变量操作的“原子类”;

2).java.util.concurrent.atomic.AtomicLong:对long变量操作的“原子类”;

3).java.util.concurrent.atomic.AtomicBoolean:对boolean变量操作的“原子类”;

它们可以保证对“变量”操作的:原子性、有序性、可见性。

AtomicInteger类示例

我们可以通过AtomicInteger类,来看看它们是怎样工作的
//线程类:

public class MyThread extends Thread {
     //static int a = 0;
    static AtomicInteger a = new AtomicInteger();// 表示整数0

    @Override
    public void run() {
        for (int i = 0; i < 200000; i++) {
            // a++;
            a.getAndIncrement();
        }
        System.out.println("子线程执行完毕");
    }
}

//测试类:

public class Test {
    public static void main(String[] args) throws InterruptedException {

        // 案例:一条子线程和一条主线程都对共享变量a进行++操作,每条线程对a++操作400000次
        new MyThread().start();

        for (int i = 0; i < 200000; i++) {
            //MyThread.a++;
            MyThread.a.getAndIncrement();
        }

        // 为了保证子线程和主线程都执行完毕
        Thread.sleep(3000);

        // 打印最终共享变量a的值(子线程,主线程对a的操作都执行完毕了)
        System.out.println("最终:"+ MyThread.a.get());

  }
}

我们能看到,无论程序运行多少次,其结果总是正确的!

AtomicInteger类的工作原理-CAS机制

在这里插入图片描述

AtomicIntegerArray类示例

  • 常用的数组操作的原子类:
    1).java.util.concurrent.atomic.AtomicIntegerArray:对int数组操作的原子类。 int[]
    2).java.util.concurrent.atomic.AtomicLongArray:对long数组操作的原子类。long[]
    3).java.util.concurrent.atomic.AtomicReferenceArray:对引用类型数组操作的原子类。Object[]
  • 数组的多线程并发访问的安全性问题:

//线程类:

public class MyThread extends Thread {
    public static int[] arr = new int[1000];

    @Override
    public void run() {
        for (int i = 0; i < arr.length(); i++) {
            arr[i]++;
        }
        System.out.println("结束");
    }
}

//测试类:

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new MyThread().start();//创建1000个线程,每个线程为数组的每个元素+1
        }

        Thread.sleep(1000 * 5);//让所有线程执行完毕

        System.out.println("主线程休息5秒醒来");
        for (int i = 0; i < MyThread.arr.length; i++) {
            System.out.println(MyThread.arr[i]);
        }
    }
}

//正常情况,数组的每个元素最终结果应为:1000,而实际打印:

1000
1000
1000
1000
999
999
999
999
999
999
999
999
1000
1000
1000
1000

可以发现,有些元素并不是1000.

//为保证数组的多线程安全,改用AtomicIntegerArray类,演示:
//线程类:

public class MyThread extends Thread {
 
    //改用原子类,使用数组构造
    public static AtomicIntegerArray arr = new AtomicIntegerArray(1000);
    @Override
    public void run() {
        for (int i = 0; i < arr.length(); i++) {
            arr.addAndGet(i, 1);//将i位置上的元素 + 1
        }
        System.out.println("结束");
    }
}

  
> 1. 测试类:
  
  ```java
  public class Demo {
      public static void main(String[] args) throws InterruptedException {
          for (int i = 0; i < 1000; i++) {
              new MyThread().start();
          }
          Thread.sleep(1000 * 5);//让所有线程执行完毕
  
          System.out.println("主线程休息5秒醒来");
          for (int i = 0; i < MyThread.arr.length(); i++) {
              System.out.println(MyThread.arr.get(i));
          }
      }
  }

先在能看到,每次运行的结果都是正确的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值