Atomic系列类封装了一系列的基础类型和对象操作,其主要目的就是为了实现原子性,主要核心类如下:
AtomicInteger
AtomicLong
AtomicBoolean
AtomicIntegerArray
AtomicLongArray
AtomicReference
原子性,多线程中能并发操作,保证数据准确
AtomicInteger与synchronized(将add操作变成一个原子操作)能保证原子性操作,(最终结果1000)能保证方法内运行中不会出现617,613之类的数据
package com.bfxy.thread.core.cas;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class UseAtomic {
private static /* int count = 0; */ AtomicInteger count = new AtomicInteger(0);
public synchronized int add(){
//return count.addAndGet(10);
count.addAndGet(3); // + 3
count.addAndGet(4); // + 4
count.addAndGet(2); // + 2
count.addAndGet(1); // + 1
// 写了一段代码逻辑........ count.add(1);
// 又写了一个复杂的逻辑...
// count.add(1);
// = 2
return count.get();
}
public static void main(String[] args) {
UseAtomic ua = new UseAtomic();
List<Thread> list = new ArrayList<>();
//如果使用atomicIntger 最终的结果 一定是: 1000
for(int i =0; i < 100; i++) {
list.add(new Thread(new Runnable() {
@Override
public void run() {
System.err.println("累计结果:" + ua.add());
}
}));
}
for(Thread t : list) {
t.start();
}
}
}
AtomicReference引用对象
package com.bfxy.thread.core.cas;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "[name: " + this.name + ", age: " + this.age + "]";
}
}
测试:
package com.bfxy.thread.core.cas;
public class UseAtomicReference1 {
private static Person person;
public static void main(String[] args) throws InterruptedException {
person = new Person("Tom", 18);
System.out.println("Person is " + person.toString());
Thread t1 = new Thread(new Task1());
Thread t2 = new Thread(new Task2());
t1.start();
t2.start();
t1.join();
t2.join();
Thread.sleep(100);
System.out.println("Now Person is " + person.toString());
}
static class Task1 implements Runnable {
public void run() {
person.setAge(19);
person.setName("Tom1");
System.out.println("Thread1 Values "
+ person.toString());
}
}
static class Task2 implements Runnable {
public void run() {
person.setAge(20);
person.setName("Tom2");
System.out.println("Thread2 Values "
+ person.toString());
}
}
}
测试2:
package com.bfxy.thread.core.cas;
import java.util.concurrent.atomic.AtomicReference;
public class UseAtomicReference2 {
// 普通引用
private static Person person;
// 原子性引用
private static AtomicReference<Person> aRperson;
public static void main(String[] args) throws InterruptedException {
person = new Person("Tom", 18);
aRperson = new AtomicReference<Person>(person);
System.out.println("Atomic Person is " + aRperson.get().toString());
Thread t1 = new Thread(new Task1());
Thread t2 = new Thread(new Task2());
t1.start();
t2.start();
t1.join();
t2.join();
Thread.sleep(500);
System.out.println("Now Atomic Person is " + aRperson.get().toString());
}
static class Task1 implements Runnable {
public void run() {
System.err.println("ret = " +
// C A S 原子操作
aRperson.compareAndSet( //10ms
aRperson.get(),
new Person("Tom", aRperson.get().getAge() + 1)
));
System.out.println("Thread1 Atomic References "
+ aRperson.get().toString());
}
}
static class Task2 implements Runnable {
public void run() {
System.err.println("ret = " +
aRperson.compareAndSet( //8ms
aRperson.get(),
new Person("Tom", aRperson.get().getAge() + 2)
));
System.out.println("Thread2 Atomic References "
+ aRperson.get().toString());
}
}
}
版权声明:本文为qq_39246466原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。