标签:变化 就是 必须 cas als this 解释 runnable 数据
public class AtomicReference<V> implements java.io.Serializable
package automic;
import java.util.concurrent.atomic.AtomicReference;
public class AtomicRefrenceTest {
public final static AtomicReference<String> atomicString = new AtomicReference<String>("gosaint");
/**
* 创建了10个线程,同时去修改atomicString的值,但是在并发状态下只有一个可以修改成功!
* @param args
*/
public static void main(String[] args) {
// 开启10个线程
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(Math.abs((int) Math.random() * 100));
} catch (Exception e) {
e.printStackTrace();
}
if (atomicString.compareAndSet("gosaint", "mrc")) {
System.out.println(Thread.currentThread().getId() + "Change value");
} else {
System.out.println(Thread.currentThread().getId() + "Failed");
}
}
}).start();
}
}
}
10Change value
9Failed
11Failed
13Failed
14Failed
15Failed
12Failed
16Failed
18Failed
17Failed
private static class Pair<T> {
final T reference;
final int stamp;
private Pair(T reference, int stamp) {
this.reference = reference;
this.stamp = stamp;
}
static <T> Pair<T> of(T reference, int stamp) {
return new Pair<T>(reference, stamp);
}
}
public boolean compareAndSet(V expectedReference,
V newReference,
int expectedStamp,
int newStamp) {
Pair<V> current = pair;
return
expectedReference == current.reference &&
expectedStamp == current.stamp &&
((newReference == current.reference &&
newStamp == current.stamp) ||
casPair(current, Pair.of(newReference, newStamp)));
}
package automic;
import java.util.concurrent.atomic.AtomicStampedReference;
public class AtomicTest {
/**
* AtomicStampedReference(V initialRef, int initialStamp)
* 创建具有给定初始值的新 AtomicStampedReference。
* @param args
*/
//创建AtomicStampedReference,用户的账户是19,版本号是0
static AtomicStampedReference<Integer> asr=new AtomicStampedReference<Integer>(19,0);
public static void main(String[] args) {
//创建3个线程给用户充话费
for(int i=0;i<3;i++){
final int expectedStamp = asr.getStamp();//获取时间戳
new Thread(new Runnable() {
@Override
public void run() {
while(true){
while(true){
//获取值
Integer expectedReference = asr.getReference();
/**
* 小于20元话费,那么充值20
*/
if(expectedReference<20){
if(asr.compareAndSet(expectedReference, expectedReference+20, expectedStamp, expectedStamp+1)){
System.out.println("充值成功,余额为:"+asr.getReference());
break;
}
}else{
break;
}
}
}
}
}).start();
}
//启动100个线程消费
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<100;i++){
while(true){
int stamp = asr.getStamp();
Integer reference = asr.getReference();
if(reference>10){
if(asr.compareAndSet(reference, reference-10, stamp, stamp+1)){
System.out.println("消费10元,余额:"+ asr.getReference());
break;
}
}else{
break;
}
}
try{
Thread.sleep(100);
}catch(Exception e){
}
}
}
}).start();
}
}
充值成功,余额为:39
消费10元,余额:29
消费10元,余额:19
消费10元,余额:9
package automic;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;
public class AtomicTest2 {
/**
* AtomicStampedReference(V initialRef, int initialStamp)
* 创建具有给定初始值的新 AtomicStampedReference。
* @param args
*/
//创建AtomicStampedReference,用户的账户是19,版本号是0
static AtomicReference<Integer> asr=new AtomicReference<Integer>(19);
public static void main(String[] args) {
//创建3个线程给用户充话费
for(int i=0;i<3;i++){
new Thread(new Runnable() {
@Override
public void run() {
while(true){
while(true){
//获取值
Integer expectedReference = asr.get();
/**
* 小于20元话费,那么充值20
*/
if(expectedReference<20){
if(asr.compareAndSet(expectedReference, expectedReference+20)){
System.out.println("充值成功,余额为:"+asr.get());
break;
}
}else{
break;
}
}
}
}
}).start();
}
//启动100个线程消费
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<100;i++){
while(true){
Integer reference = asr.get();
if(reference>10){
if(asr.compareAndSet(reference, reference-10)){
System.out.println("消费10元,余额:"+ asr.get());
break;
}
}else{
break;
}
}
try{
Thread.sleep(100);
}catch(Exception e){
}
}
}
}).start();
}
}
充值成功,余额为:39
消费10元,余额:29
消费10元,余额:19
充值成功,余额为:39
消费10元,余额:29
消费10元,余额:19
充值成功,余额为:39
消费10元,余额:29
充值成功,余额为:39
消费10元,余额:39
消费10元,余额:29
充值成功,余额为:39
消费10元,余额:39
标签:变化 就是 必须 cas als this 解释 runnable 数据
原文地址:https://www.cnblogs.com/gosaint/p/9068195.html