【Beautiful JUC Part.5】atomic包 一刻也不能分割
【Beautiful JUC Part.5】atomic包 一刻也不能分割
一、什么是原子类
- 不可分割
- 一个操作是不可中断的,即便是多线程的情况下也可以保证
- java.util.concurrent.atomic
- 原子类的作用和锁类似,是为了保证并发情况下线程安全。不过原子类相比于锁,有一定的优势。
- 粒度更细:原子变量可以把竞争范围缩小到变量级别,这是我们可以获得的最细粒度的情况了,通常锁的粒度都要大于原子变量的粒度
- 效率更高:通常,使用原子类的效率会比使用锁的效率更高,除了高度竞争的情况
二、AtomicInteger案例演示
1、AtomicInteger常用方法
-
public final int get() //获取当前的值
-
public final int getAndSet(int newValue)//获取当前的值
-
public final int getAndIncrement()//获取当前的值,并自增
-
public final int getAndDecrement()//获取当前的值,并自减
-
public final int getAndAdd(int delta)//获取当前的值,并加上预期的值
-
boolean compareAndSet(int expect, int update)//如果输入的数值等于预期值,则以原子方式将该值设置为输入值(update)
2、演示getAndIncrement()
/**
* 描述: 演示AtomicInteger的基本用法,对比非原子类的线程安全问题,
* 使用了原子类之后,不需要加锁,也可以保证线程安全
*/
public class AtomicIntegerDemo1 implements Runnable{
private static AtomicInteger atomicInteger = new AtomicInteger();
public void incrementAtomic() {
atomicInteger.getAndIncrement();
}
private static volatile int basicCount = 0;
public void incrementBasic() {
basicCount++;
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
incrementAtomic();
incrementBasic();
}
}
public static void main(String[] args) throws InterruptedException {
AtomicIntegerDemo1 r = new AtomicIntegerDemo1();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("原子类的结果是:" + atomicInteger.get());
System.out.println("普通变量的结果:" + basicCount);
}
}
三、AtomicIntegerArray演示
/**
* 描述:演示原子数组的使用方法
*/
public class AtomicArrayDemo {
public static void main(String[] args) throws InterruptedException {
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(1000);
Incrementer incrementer = new Incrementer(atomicIntegerArray);
Decrementer decrementer = new Decrementer(atomicIntegerArray);
Thread[] threadsIncrementer = new Thread[100];
Thread[] threadsDecrementer = new Thread[100];
for (int i = 0; i < 100; i++) {
threadsDecrementer[i] = new Thread(decrementer);
threadsIncrementer[i] = new Thread(incrementer);
threadsDecrementer[i].start();
threadsIncrementer[i].start();
}
//Thread.sleep(1000);
for (int i = 0; i < 100; i++) {
threadsDecrementer[i].join();
threadsIncrementer[i].join();
}
for (int i = 0; i < atomicIntegerArray.length(); i++) {
//if (atomicIntegerArray.get(i) != 0) {
// System.out.println("发现了错误" + i);
//}
System.out.println(atomicIntegerArray.get(i));
}
System.out.println("运行结束");
}
}
class Decrementer implements Runnable {
private AtomicIntegerArray atomicIntegerArray;
public Decrementer(AtomicIntegerArray atomicIntegerArray) {
this.atomicIntegerArray = atomicIntegerArray;
}
@Override
public void run() {
for (int i = 0; i < atomicIntegerArray.length(); i++) {
//进行自减操作
atomicIntegerArray.getAndDecrement(i);
}
}
}
class Incrementer implements Runnable {
private AtomicIntegerArray atomicIntegerArray;
public Incrementer(AtomicIntegerArray atomicIntegerArray) {
this.atomicIntegerArray = atomicIntegerArray;
}
@Override
public void run() {
for (int i = 0; i < atomicIntegerArray.length(); i++) {
//进行自减操作
atomicIntegerArray.getAndIncrement(i);
}
}
}
四、Atomic*Reference引用类型原子类
AtomicReference:AtomicReference类的作用,和AtomicInteger并没有本质区别,AtomicInteger可以让一个整数保证原子性,而AtomicReference可以让一个对象保证原子性,当然,AtomicReference的功能明显比AtomicInteger强,因为一个对象里可以包含很多属性。用法和AtomicInteger类似。
自旋锁代码演示
import java.util.concurrent.atomic.AtomicReference;
public class SpinLock {
private AtomicReference<Thread> sign = new AtomicReference<>();
public void lock() {
Thread current = Thread.currentThread();
//如果没有设置,就把当前这个线程设置为这个对象的引用
while (!sign.compareAndSet(null, current)) {
System.out.println(Thread.currentThread().getName() + "自旋获取失败,再次尝试");
}
}
public void unlock() {
Thread current = Thread.currentThread();
//释放掉当前这个对象的引用
sign.compareAndSet(current, null);
}
public static void main(String[] args) {
SpinLock spinLock = new SpinLock();
Runnable runnable = new Runnable(){
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "开始尝试获取自旋锁");
spinLock.lock();
System.out.println(Thread.currentThread().getName() + "获取到了自旋锁");
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
spinLock.unlock();
System.out.println(Thread.currentThread().getName() + "释放了锁");
}
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
}
}
五、把普通变量升级为具有原子功能的变量
AtomicIntegerFieIdUpdater对普通变量进行升级
使用场景:普通变量偶尔需要一个原子的get-set操作
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
/**
* 描述:演示AtomicIntegerFieldUpdater的用法
*/
public class AtomicIntegerFieldUpdaterDemo implements Runnable{
static Candidate tom;
static Candidate peter;
//候选人
public static class Candidate{
volatile int score;
}
public static AtomicIntegerFieldUpdater<Candidate> scoreUpdater =
AtomicIntegerFieldUpdater.newUpdater(Candidate.class, "score");
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
peter.score++;
scoreUpdater.getAndIncrement(tom);
}
}
public static void main(String[] args) throws InterruptedException {
AtomicIntegerFieldUpdaterDemo r = new AtomicIntegerFieldUpdaterDemo();
tom = new Candidate();
peter = new Candidate();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("普通的变量" + peter.score);
System.out.println("升级后的结果" + tom.score);
}
}
注意点:
- 可见范围
- 遍历一定是可见的,如果不可见private是不行的,因为这里我们用到了反射的调用
- 如果遍历是静态的(static)或者没有加volatile关键字是不行的。
六、累加器
1、Adder累加器
Java8引入的,是一个比较新的类
高并发下LongAdder比AtomicLong效率高,不过本质是空间换时间
竞争激烈的时候,LongAdder把不同线程对应到不同的Cell上进行修改,降低了冲突的概率,是多段锁的理念,提高了并发性。
AtomicLong代码演示
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
/**
* 描述;演示高并发场景下,LongAdder比AtomicLong性能好
*/
public class AtomicLongDemo {
public static void main(String[] args) throws InterruptedException {
AtomicLong counter = new AtomicLong(0);
ExecutorService service = Executors.newFixedThreadPool(20);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
service.submit(new Task(counter));
}
service.shutdown();
while (!service.isTerminated()) {
}
long end = System.currentTimeMillis();
System.out.println(counter.get());
System.out.println("AtomicLong耗时: " + (end - start) + "毫秒");
}
private static class Task implements Runnable {
private AtomicLong counter;
public Task(AtomicLong counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
counter.incrementAndGet();
}
}
}
}
LongAdder代码演示
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
/**
* 描述: 演示高并发场景下,LongAdder比AtomicLong性能好
*/
public class LongAdderDemo {
public static void main(String[] args) throws InterruptedException {
LongAdder counter = new LongAdder();
ExecutorService service = Executors.newFixedThreadPool(20);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
service.submit(new Task(counter));
}
service.shutdown();
while (!service.isTerminated()) {
}
long end = System.currentTimeMillis();
System.out.println(counter.sum());
System.out.println("LongAdder耗时:" + (end - start) + "毫秒");
}
private static class Task implements Runnable {
private LongAdder counter;
public Task(LongAdder counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
counter.increment();
}
}
}
}
2、分析高速运转的原理
AtomicLong的对象,在多线程竞争激烈的时候,每一次加法,都要flush和refresh,导致很耗费资源。
LongAdder带来的改进和原理