天天看点

高并发编程之AtomicLong讲解

一、AtomicLong介绍

AtomicLong是作用是对长整形进行原子操作。

在32位操作系统中,64位的long 和 double 变量由于会被JVM当作两个分离的32位来进行操作,所以不具有原子性。而使用AtomicLong能让long的操作保持原子型。

二、AtomicLong的几个常用方法

①.创建具有初始值 0 的新 AtomicLong。

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //1、创建具有初始值 0 的新 AtomicLong。
        AtomicLong atomicLong = new AtomicLong();
        System.out.println("Value:" + atomicLong.get());
    }
}

           

②.创建具有给定初始值的新 AtomicLong。

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //2、创建具有给定初始值的新 AtomicLong。
        AtomicLong atomicLong1 = new AtomicLong(10);
        System.out.println("Value:" + atomicLong1.get());
    }
}

           

③.addAndGet()方法:以原子方式将给定值添加到当前值,先加上特定的值,再获取结果

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //3、以原子方式将给定值添加到当前值,先加上特定的值,再获取结果
        AtomicLong atomicLong2 = new AtomicLong(3);
        atomicLong2.addAndGet(5);
        System.out.println("Value:" + atomicLong2.get());
    }
}

           

④.getAndAdd()方法:先获取当前值再加上特定的值

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //4、先获取当前值再加上特定的值
        AtomicLong atomicLong5 = new AtomicLong(10);
        atomicLong5.getAndAdd(5);
        System.out.println("Value:" + atomicLong5.get());
    }
}

           

⑤.compareAndSet()方法:如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //5、如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
        AtomicLong atomicLong3 = new AtomicLong(10);
        atomicLong3.compareAndSet(10,15);
        System.out.println("Value:" + atomicLong3.get());
    }
}

           

⑥.decrementAndGet()方法:以原子方式将当前值减 1,先减去1再获取值

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //6、 以原子方式将当前值减 1,先减去1再获取值
        AtomicLong atomicLong4 = new AtomicLong(10);
        atomicLong4.decrementAndGet();
        System.out.println("Value:" + atomicLong4.get());
    }
}

           

⑦.getAndDecrement()方法:先获取当前值再减1

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //7、先获取当前值再减1
        AtomicLong atomicLong6 = new AtomicLong();
        atomicLong6.getAndDecrement();
        System.out.println("Value:" + atomicLong6.get());
    }
}

           

⑧.getAndIncrement()方法:先获取当前值再加1

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //8、先获取当前值再加1
        AtomicLong atomicLong7 = new AtomicLong();
        atomicLong7.getAndIncrement();
        System.out.println("Value:" + atomicLong7.get());
    }
}

           

⑨.incrementAndGet()方法:先加1再获取当前值

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //9、先加1再获取当前值
        AtomicLong atomicLong9 = new AtomicLong();
        atomicLong9.incrementAndGet();
        System.out.println("Value:" + atomicLong9.get());
    }
}

           

⑩.getAndSet()方法:先获取当前值再设置新的值

package chapter3.AtomicLongTest;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author czd
 */
public class AtomicLongTest {
    public static void main(String[] args) {
        //10、先获取当前值再设置新的值
        AtomicLong atomicLong8 = new AtomicLong();
        atomicLong8.getAndSet(20);
        System.out.println("Value:" + atomicLong8.get());
    }
}

           

继续阅读