使用原子的方式更新基本类型,Atomic包提供了以下3个类。
AtomicInteger常用方法如下:
代码示例:
static AtomicInteger ai = new AtomicInteger(1);
public static void main(String[] args) {
AtomicIntegerTest();
}
public static void AtomicIntegerTest() {
System.out.println(ai.getAndIncrement());
System.out.println(ai.get());
}
运行结果:
1
2
getAndIncrement的源码如代码:
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next)) return current;
}
}
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
源码中for循环体的第一步先取得AtomicInteger里存储的数值,第二步对AtomicInteger的当前数值进行加1操作,关键的第三步调用compareAndSet方法来进行原子更新操作,该方法先检查当前数值是否等于current,等于意味着AtomicInteger的值没有被其他线程修改过,则将AtomicInteger的当前数值更新成next的值,如果不等compareAndSet方法会返回false,程序会进入for循环重新进行compareAndSet操作。
AtomicIntegerArray类主要是提供原子的方式更新数组里的整型,其常用方法如下:
实例代码如代码:
static AtomicIntegerArray aiA = new AtomicIntegerArray(5);
public static void AtomicIntegerArrayTest() {
System.out.println(aiA.addAndGet(2, 5));
System.out.println(aiA.get(1));
System.out.println(aiA.compareAndSet(1, 0, 9));
System.out.println(aiA.get(1));
System.out.println(aiA.getAndSet(0, 3));
System.out.println(aiA.get(0));
System.out.println(aiA.length());
}
public static void main(String[] args) {
AtomicIntegerArrayTest();
}
运行结果:
5
0
true
9
0
3
5
代码示例:
static class User {
private String name;
private int old;
//get set 构造 略
}
public static AtomicReference atomicUserRef = new AtomicReference();
public static void main(String[] args) {
User user = new User("conan", 15);
atomicUserRef.set(user);
User updateUser = new User("Shinichi", 17);
atomicUserRef.compareAndSet(user, updateUser);
System.out.println(atomicUserRef.get().getName());
System.out.println(atomicUserRef.get().getOld());
System.out.println("=========================");
}
运行结果:
Shinichi
17
=========================
AstomicIntegerFieldUpdater的示例代码如代码:
// 创建原子更新器,并设置需要更新的对象类和对象的属性
private static AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater
.newUpdater(User.class, "old");
public static void main(String[] args) {
// 设置柯南的年龄是10岁
User conan = new User("conan", 10);
// 柯南长了一岁,但是仍然会输出旧的年龄
System.out.println(a.getAndIncrement(conan));
// 输出柯南现在的年龄
System.out.println(a.get(conan));
}
public static class User {
private String name;
public volatile int old;
public User(String name, int old) {
this.name = name;
this.old = old;
}
public String getName() {
return name;
}
public int getOld() {
return old;
}
}
运行结果:
10
11
ABA问题代码示例:
public class AtomicReferenceABATest {
public final static AtomicReference ATOMIC_REFERENCE = new AtomicReference("abc");
public static void main(String []args) {
for(int i = 0 ; i < 100 ; i++) {
final int num = i;
new Thread() {
public void run() {
try {
Thread.sleep(Math.abs((int)(Math.random() * 100)));
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ATOMIC_REFERENCE.compareAndSet("abc" , "abc2")) {
System.out.println( ATOMIC_REFERENCE.get());
System.out.println("我是线程:" + num + ",我获得了锁进行了对象修改!");
}
}
}.start();
}
new Thread() {
public void run() {
while(!ATOMIC_REFERENCE.compareAndSet("abc2", "abc"));
System.out.println( ATOMIC_REFERENCE.get());
System.out.println("已经改为原始值!");
}
}.start();
}
}
运行结果:
abc2
我是线程:1,我获得了锁进行了对象修改!
abc
已经改为原始值!
abc2
我是线程:59,我获得了锁进行了对象修改!