java-----原子操作

本篇只记录原子操作的基本知识,并通过一个例子展示其使用的方便。

如果想深入理解请看下面这篇:

https://blog.csdn.net/silently_frog/article/details/100558108

原子操作的作用:在多线程中,让一个共享变量的值同步。

原子操作的类型:

               基本原子类型(AtomicInteger、AtomicLong、AtomicBoolean)、

               原子数组类型、原子引用类型、原子属性修改器、原子计算器

常用方法有:

    1. int addAndGet(int delta):以原子的方式将输入的值与实例中的值相加,并把结果返回

    2. boolean compareAndSet(int expect, int update):如果输入值等于预期值,以原子的方式将该值设置为输                                      入的值

    3. final int getAndIncrement():以原子的方式将当前值加1,并返回加1之前的值

    4. void lazySet(int newValue):最终会设置成newValue,使用lazySet设置值后,可能导致其他线程在之后的一小段时                       间内还是可以读到旧的值。

    5. int getAndSet(int newValue):以原子的方式将当前值设置为newValue,并返回设置之前的旧值

              6. public final void set(int newValue)设置新的数据

              7. public final  T get() 获取原始数据内容

   使用 AtomicInteger与普通变量对比:

class Count {
    private  AtomicInteger num = new AtomicInteger(30);
    private int i=30;
    public int add() {
      return (this.num.decrementAndGet()+1);  //减法操作
    }

    public int addNormal() {
        this.i--;
        return (this.i+1);
    }

}


public class myClass {
    public static void main(String[] args) throws InterruptedException {
        Count count = new Count();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 15; i++) {
                    System.out.println("1计算结果:" + count.add());

                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 15; i++) {

                    System.out.println("2计算结果:" +count.add());

                }
            }
        }).start();


//普通
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    TimeUnit.MILLISECONDS.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                for (int i = 0; i < 10; i++) {
//                    System.out.println("3计算结果:" + count.addNormal());
//                }
//            }
//        }).start();
//
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    TimeUnit.MILLISECONDS.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                for (int i = 0; i < 10; i++) {
//                    System.out.println("4计算结果:" + count.addNormal());
//                }
//            }
//        }).start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    TimeUnit.MILLISECONDS.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                for (int i = 0; i < 10; i++) {
//                    System.out.println("5计算结果:" + count.addNormal());
//                }
//            }
//        }).start();
    }
}

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值