并发编程5--原子操作CAS
程序员文章站
2022-05-03 19:18:49
...
Atom(不可分割)
什么是原子操作?如何实现原子操作?
syn基于阻塞的锁的机制,1、被阻塞的线程优先级很高,2、拿到锁的线程一直不释放锁怎么办?3、大量的竞争,消耗cpu,同时带来死锁或者其他安全。
CAS的原理
CAS(Compare And Swap),指令级别保证这是一个原子操作
三个运算符: 一个内存地址V,一个期望的值A,一个新值B
基本思路:如果地址V上的值和期望的值A相等,就给地址V赋给新值B,如果不是,不做任何操作。
循环(死循环,自旋)里不断的进行CAS操作
CAS的问题
A—》B----》A,版本号: A1B2-A3
CAS操作长期不成功,cpu不断的循环,
Jdk中相关原子操作类的使用
AtomicMarkableReference,boolean 有没有动过
AtomicStampedReference 动过几次
代码如下:
package com;
import java.util.concurrent.atomic.AtomicIntegerArray;
/**
*
* 当使用锁开销比较大,可以使用原子操作类
*/
public class AtomicArray {
static int[] value = new int[] { 1, 2 };
static AtomicIntegerArray ai = new AtomicIntegerArray(value);
public static void main(String[] args) {
ai.getAndSet(0, 3);
System.out.println(ai.get(0));
System.out.println(value[0]);
}
}
package com;
import java.util.concurrent.atomic.AtomicInteger;
public class UseAtomicInt {
static AtomicInteger ai = new AtomicInteger(10);
public static void main(String[] args) {
System.out.println(ai.getAndIncrement());//10--->11
System.out.println(ai.incrementAndGet());//11--->12--->out
System.out.println(ai.get());
}
}
package com;
import java.util.concurrent.atomic.AtomicReference;
/*
*
*类说明:演示引用类型的原子操作类
*/
public class UseAtomicReference {
static AtomicReference<UserInfo> userRef = new AtomicReference<UserInfo>();
public static void main(String[] args) {
UserInfo user = new UserInfo("Mark", 15);//要修改的实体的实例
userRef.set(user);
UserInfo updateUser = new UserInfo("Bill", 17);//要变化的新实例
userRef.compareAndSet(user, updateUser);
System.out.println(userRef.get().getName());
System.out.println(userRef.get().getAge());
System.out.println(user.getName());
System.out.println(user.getAge());
}
//定义一个实体类
static class UserInfo {
private String name;
private int age;
public UserInfo(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
}
package com;
import java.util.concurrent.atomic.AtomicStampedReference;
/**
*
*类说明:演示带版本戳的原子操作类
*/
public class UseAtomicStampedReference {
static AtomicStampedReference<String> asr =
new AtomicStampedReference<>("Mark",0);
public static void main(String[] args) throws InterruptedException {
final int oldStamp = asr.getStamp();//那初始的版本号
final String oldReferenc = asr.getReference();
System.out.println(oldReferenc+"==========="+oldStamp);
Thread rightStampThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()
+"当前变量值:"+oldReferenc+"当前版本戳:"+oldStamp+"-"
+asr.compareAndSet(oldReferenc, oldReferenc+"Java",
oldStamp, oldStamp+1));
}
});
Thread errorStampThread = new Thread(new Runnable() {
@Override
public void run() {
String reference = asr.getReference();
System.out.println(Thread.currentThread().getName()
+"当前变量值:"+reference+"当前版本戳:"+asr.getStamp()+"-"
+asr.compareAndSet(reference, reference+"C",
oldStamp, oldStamp+1));
}
});
rightStampThread.start();
rightStampThread.join();
errorStampThread.start();
errorStampThread.join();
System.out.println(asr.getReference()+"==========="+asr.getStamp());
}
}
上一篇: 结婚这么些年
下一篇: 1.2.2 线程安全之原子操作CAS