什么是原子变量
原子变量类基于CAS实现的,当对共享变量进行read-modify-write更新操作时,通过原子变量类可以保障操作的原子性与可见性,对变量的read-modify-write更新操作是指当前操作不是一个简单的赋值,而是变量的新值依赖变量的旧值,如自增操作i++,由于volatile只能保证可见性,无法保障原子性,原子变量类的内部就是借助一个volatile变量,并且保障了该变量的read-modify-write操作的原子性,优势把原子变量类看做增强的volatile变量
原子变量类有12个
类型 | 类名 |
---|---|
基础数据型 | AtomicInterger,AtomicLong,AtomicBoolean |
数组型 | AtomicIntegerArray,AtomicLongArray,AtomicReferenceArray |
字段更新器 | AtomicIntegerFielfUpdater,AtomicLongFieldUpdater,AtomicReferenceFieldUpdater |
引用型 | AtomicReference,AtomicStampedReference,AtomicMarkableReference |
一.使用AtomicLong定义计数器
1)定义单例Indicator
package com.dome.Atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author qb
* @version 1.0
* @Description
* 使用原子变量类定义一个计数器
* 该计数器在整个程序中都能使用,并且所有的地方都使用这一个计数器,这个计数器就可以设计为单例
* @date 2021/3/9 15:22
*/
public class Indicator {
/**
* 1.构造方法私有化
*/
private Indicator(){};
/**
* 定义一个私有的本类静态变量
*/
private static final Indicator INDICATOR = new Indicator();
/**
* 提供一个公共静态方法返回该类的唯一实例
*/
public static Indicator getIndicator(){
return INDICATOR;
}
/**
* 使用原子变量类保存请求总数,成功数,失败数
*/
private final AtomicLong requestCount = new AtomicLong(0); //请求总数
private final AtomicLong successCount = new AtomicLong(0); //成功总数
private final AtomicLong fialureCount = new AtomicLong(0); //失败总数
/**
* 有新的请求
*/
public void newRequestReceive(){
requestCount.incrementAndGet();
}
/**
* 成功
*/
public void requestProcessSuccess(){
successCount.incrementAndGet();
}
/**
* 失败
*/
public void requestProcessFailure(){
fialureCount.incrementAndGet();
}
/**
* 查看总数,失败数,成功数
*/
public long getRequestCount(){
return requestCount.get();
}
public long getSuccessCount(){
return successCount.get();
}
public long getFialureCount(){
return fialureCount.get();
}
}
2).test方法
package com.dome.Atomic.AtomicLong;
import java.util.Random;
/**
* @author qb
* @version 1.0
* @Description
* 模拟服务器的请求总数,成功数,失败数
* @date 2021/3/9 15:20
*/
public class test {
public static void main(String[] args) {
//通过线程模拟请求,实际应用中可以通过 servletFilter中调用Indicator计数器的相关方法
for (int i=0;i<10000;i++){
new Thread(new Runnable() {
@Override
public void run() {
//每个线程就是一个请求,请求总数加1
Indicator.getIndicator().newRequestReceive();
int num = new Random().nextInt();
if(num % 2 == 0){
//偶数;来模拟请求成功
Indicator.getIndicator().requestProcessSuccess();
}else{
//请求失败
Indicator.getIndicator().requestProcessFailure();
}
}
}).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//总数
System.out.println("总数:"+Indicator.getIndicator().getRequestCount());
System.out.println("成功:"+Indicator.getIndicator().getSuccessCount());
System.out.println("失败:"+Indicator.getIndicator().getFialureCount());
}
}
3).执行结果
执行的结果失败成功想加便是总数
二:AtomicIntegerArray的基本操作
package com.dome.Atomic.AtomicArray;
import java.util.concurrent.atomic.AtomicIntegerArray;
/**
* @author qb
* @version 1.0
* @Description
* AtomicIntegerArray
* 原子更新数组的基本操作
* @date 2021/3/9 15:42
*/
public class test {
public static void main(String[] args) {
//1.创建一个指定长度的原子数组
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);
System.out.println(atomicIntegerArray);
//2.返回指定位置的元素
System.out.println(atomicIntegerArray.get(0)); //0
System.out.println(atomicIntegerArray.get(1)); //0
//3.设置指定位置的元素
atomicIntegerArray.set(0,10);
//如果在设置数组元素的新值是,可以先返回数组元素的旧值
System.out.println(atomicIntegerArray.getAndSet(1,11)); //0
System.out.println(atomicIntegerArray); //[10, 11, 0, 0, 0, 0, 0, 0, 0, 0]
//4. 修改数组元素的值,把数组元素加上某个数
System.out.println(atomicIntegerArray.addAndGet(0,22)); //32
//先返回原来的值,在增加
System.out.println(atomicIntegerArray.getAndAdd(1,33));//44
System.out.println(atomicIntegerArray);//[32, 44, 0, 0, 0, 0, 0, 0, 0, 0]
//5. CAS操作 如果数组中索引值为0的元素的值是32,我们就修改为222
System.out.println(atomicIntegerArray.compareAndSet(0,32,222)); //true
System.out.println(atomicIntegerArray);//[222, 44, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(atomicIntegerArray.compareAndSet(1,11,333));//false
System.out.println(atomicIntegerArray); //[222, 44, 0, 0, 0, 0, 0, 0, 0, 0]
//6.自增/自减
System.out.println(atomicIntegerArray.incrementAndGet(0)); //223 增加再返回
System.out.println(atomicIntegerArray.getAndIncrement(1)); //44 先返回再增加
System.out.println(atomicIntegerArray);//[223, 45, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println(atomicIntegerArray.decrementAndGet(2)); //-1 先减再返回
System.out.println(atomicIntegerArray.getAndDecrement(3)); // 0 先返回再减
System.out.println(atomicIntegerArray);
}
}
三:多线程中使用原子数组
package com.dome.Atomic.AtomicArray;
import java.util.concurrent.atomic.AtomicIntegerArray;
/**
* @author qb
* @version 1.0
* @Description
* 在多线程中使用AtomicIntegerArray 原子数组
* @date 2021/3/9 16:00
*/
public class test02 {
//定义原子数据
static AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);
public static void main(String[] args) {
//定义一个线程数组
Thread[] threads = new Thread[10];
for (int i=0;i<threads.length;i++){
threads[i] = new AddThread();
}
//开启子线程
for (Thread thread:threads){
thread.start();
}
//在主线程中查看,自增完以后原子数组中的各个元素的值,在主线程中需要所有子线程都执行完后在查看
//可以将所有的子线程合并到当前的主线程中
for (Thread thread:threads){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(atomicIntegerArray);
}
//定义一个线程类,在线程中修改原子数组
static class AddThread extends Thread{
@Override
public void run() {
//把原子数组的每个元素自增1000次
for (int j=0;j<1000;j++){
for (int i=0;i<atomicIntegerArray.length();i++){
atomicIntegerArray.getAndIncrement(i % atomicIntegerArray.length());
}
}
// for (int i=0;i<10000;i++){
// atomicIntegerArray.getAndIncrement(i % atomicIntegerArray.length());
// }
}
}
}
执行结果
四:AtomicIntegerFieldUpdater
AtomicIntegerFieldUpdater可以对原子整数字段进行更新,要求:
- 字段必须使用volatile修饰,是线程之间可见
- 只能是实例变量,不能是静态变量,不能使用final修饰
1.)定义User类
package com.dome.Atomic.AtomicIntegerFiledUpdate;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author qb
* @version 1.0
* @Description
* 使用AtomicIntegerFiledUpdate更新的字段必须使用volatile修饰
* @date 2021/3/9 16:19
*/
@Data
@AllArgsConstructor
public class User {
int id;
volatile int age;
}
2).定义SubThread
package com.dome.Atomic.AtomicIntegerFiledUpdate;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
/**
* @author qb
* @version 1.0
* @Description
* @date 2021/3/9 16:21
*/
public class SubThread extends Thread{
private User user; //更新指定user
//创建AtomicIntegerFieldUpdater更新器
private AtomicIntegerFieldUpdater<User> updater =
AtomicIntegerFieldUpdater.newUpdater(User.class,"age");
public SubThread( User user) {
this.user = user;
}
@Override
public void run() {
//在子线程中对user对象的age字段自增10次
for (int i=0; i<10;i++){
System.out.println(updater.getAndIncrement(user));
}
}
}
3).test 执行main方法
package com.dome.Atomic.AtomicIntegerFiledUpdate;
/**
* @author qb
* @version 1.0
* @Description
* @date 2021/3/9 16:19
*/
public class test {
public static void main(String[] args) {
User user = new User(1234,10);
//开启10个线程
for (int i = 0;i<10;i++){
new SubThread(user).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(user.toString());
}
}
4).执行结果
五:AtomicReference原子操作对象
package com.dome.Atomic.AtomicReference;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author qb
* @version 1.0
* @Description
* 使用 AtomicReference 原子读写对象
* @date 2021/3/9 16:32
*/
public class test01 {
//创建AtomicReference对象
static AtomicReference<String> atomicReference = new AtomicReference<>("abc");
public static void main(String[] args) {
//创建100个线程,修改字符串
for (int i=0;i<100;i++){
new Thread(new Runnable() {
@Override
public void run() {
if(atomicReference.compareAndSet("abc","def")){
System.out.println(Thread.currentThread().getName() +" 把字符串更改为 def");
}
}
}).start();
}
for (int i=0;i<100;i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(new Random().nextInt(20));
} catch (InterruptedException e) {
e.printStackTrace();
}
if(atomicReference.compareAndSet("def","abc")){
System.out.println(Thread.currentThread().getName() +" 把字符串还原为 abc");
}
}
}).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(atomicReference.get());
}
}
结果
六:AtomicReference中的ABA问题
package com.dome.Atomic.AtomicReference;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author qb
* @version 1.0
* @Description
* AtomicReference 可能会出现CAS的ABA问题
* @date 2021/3/9 16:39
*/
public class test02 {
private static AtomicReference<String> atomicReference = new AtomicReference<>("abc");
public static void main(String[] args) {
//创建第一个线程 先把abc改为def,再把字符串还原为abc
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
atomicReference.compareAndSet("abc","def");
System.out.println(Thread.currentThread().getName()+"--" +atomicReference.get());
atomicReference.compareAndSet("def","abc");
System.out.println(Thread.currentThread().getName()+"--" +atomicReference.get());
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(atomicReference.compareAndSet("abc","ghg"));
}
});
t1.start();
t2.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(atomicReference.get()); //ghg
}
}
结果
应该为false,返回的确实true,所以最后打印的值为ghg
七:使用AtomicStampedReference解决CAS中的ABA问题
package com.dome.Atomic.AtomicReference;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;
/**
* @author qb
* @version 1.0
* @Description
*
* AtomicStampedReference 原子类可以解决CAS中的ABA问题
* AtomicStampedReference类中有一个整数标记值stamp,每次执行CAS操作时,需要比较他的版本,比较stamp的值
* @date 2021/3/9 16:47
*/
public class test03 {
private static AtomicReference<String> atomicReference = new AtomicReference<>("abc");
/**
* 参数一:初始值
* 参数二:版本号
*/
private static AtomicStampedReference<String> stampedReference = new AtomicStampedReference<>("abc",0);
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
stampedReference.compareAndSet("abc","def",
stampedReference.getStamp(),stampedReference.getStamp()+1);
System.out.println(Thread.currentThread().getName()+" ---" +stampedReference.getReference());
stampedReference.compareAndSet("def","abc",
stampedReference.getStamp(),stampedReference.getStamp()+1);
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
int stamp = stampedReference.getStamp(); //获得版本号 false b避免ABA问题
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//int stamp = stampedReference.getStamp(); //获得版本号 true
System.out.println(stampedReference.compareAndSet("abc","ggg",
stamp,stamp+1));
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(stampedReference.getReference());
}
}
结果
因为在睡眠之前获取的版本号,和睡眠之后的版本号不一样所以返回false,不更改数据