什么是原子类,有什么作用?
- 不可分割
- 一个操作是不可中断的,即便是多线程的情况下也可以保证
java.util.concurrent.atomic
- 原子类的作用和锁类似,是为了保证并发情况下线程安全。不过原子类相比于锁,有一定的优势
- 粒度更细: 原子变量可以把竞争范围缩小到变量级别,这是我门可以获得的最细粒度的情况了,通常锁的粒度都要大于原子变量的粒度
- 效率更高: 通常,使用原子类的效率会比使用锁的效率更高,除了高度竞争的情况
6类原子类纵览
Atomic基本类型原子类 | |
---|---|
Atomic *基本类型原子类 | AtomicInteger 、AtomicLong 、AtomicBoolean |
Atomic*Array 数组类型原子类 | AtomiclntegerArray 、AtomicLongArray 、AtomicReferenceArray |
Atomic*Reference 引用类型原子类 | AtomicReference 、AtomicStampedReference 、AtomicMarkableReference |
Atomic*FieldUpdater 升级类型原子类 | Atomiclntegerfieldupdater 、AtomicLongFieldUpdater 、AtomicReferenceFieldUpdate |
Adder 累加器 | LongAdder , DoubleAdder |
Accumulator 累加器 | LongAccumulator 、DoubleAccumulator |
Atomic
基本类型原子类,已 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
public class AtomicIntegerDemo1 implements Runnable{
private static final AtomicInteger atomicAtomicInteger = new AtomicInteger();
private static volatile int basicCount = 0;
public void incrementAtomic() {
atomicAtomicInteger.getAndIncrement();
}
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 atomicIntegerDemo1 = new AtomicIntegerDemo1();
Thread t1 = new Thread(atomicIntegerDemo1);
Thread t2 = new Thread(atomicIntegerDemo1);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("原子类的结果: " + atomicAtomicInteger.get());//20000
System.out.println("普通变量的结果: " + basicCount);//少于20000
}
}
Atomic*Array数组类型原子类
package com.zeny.threadstudy.atomics;
import java.util.concurrent.atomic.AtomicIntegerArray;
@SuppressWarnings("all")
public class AtomicArrayDemo {
public static void main(String[] args) {
int len = 100;
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(len);
Thread[] threadsIncrementer = new Thread[len];
Thread[] threadsDecrementer = new Thread[len];
Incrementer incrementer = new Incrementer(atomicIntegerArray);
Decrementer decrementer = new Decrementer(atomicIntegerArray);
for (int i = 0; i < len; i++) {
threadsIncrementer[i] = new Thread(incrementer);
threadsDecrementer[i] = new Thread(decrementer);
}
for (int i = 0; i < len; i++) {
threadsIncrementer[i].start();
threadsDecrementer[i].start();
}
while (Thread.activeCount() > 2);
for (int i = 0; i < len; i++) {
if (atomicIntegerArray.get(i) != 0) {
System.out.println("发现了错误 " + i);
}
}
System.out.println("运行结束");
}
}
class Decrementer implements Runnable {
private AtomicIntegerArray array;
public Decrementer(AtomicIntegerArray array) {
this.array = array;
}
@Override
public void run() {
for (int i = 0; i < array.length(); i++) {
array.getAndDecrement(i);
}
}
}
class Incrementer implements Runnable {
private AtomicIntegerArray array;
public Incrementer(AtomicIntegerArray array) {
this.array = array;
}
@Override
public void run() {
for (int i = 0; i < array.length(); i++) {
array.getAndIncrement(i);
}
}
}
Atomic* Reference引用类型原子类
AtomicReference
: AtomicReference
类的作用,和AtomicInteger
并没有本质区别, AtomicInteger
可以让一个整数保证原子性,而 AtomicReference
可以让一个对象保证原子性,当然, AtomicReference
的功能明显比 AtomicInteger
强,因为一个对象里可以包含很多属性,用法和AtomicInteger
类似。
public class SpinLock {
private AtomicReference<Thread> sign = new AtomicReference<>();
public void lock() {
Thread current = Thread.currentThread();
while (!sign.compareAndSet(null, current)) {
}
}
public void unlock() {
Thread current = Thread.currentThread();
sign.compareAndSet(current, null);
}
public static void main(String[] args) {
SpinLock spinLock = new SpinLock();
Runnable runnable = () -> {
System.out.println(Thread.currentThread().getName() + "开始尝试获取自旋锁");
spinLock.lock();
System.out.println(Thread.currentThread().getName() + "获取到了自旋锁");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + "释放自旋锁");
spinLock.unlock();
}
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
}
}
把普通变量升级为原子类:用 AtomicIntegerFieldUpdater
升级原有变量
◆ AtomicIntegerFieldUpdater
对普通变量进行升级
◆使用场景:偶尔需要一个原子 get-set
操作
public class AtomicIntegerFiledUpdaterDemo implements Runnable{
static Candidate tom;
static Candidate peter;
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 class Candidate {
volatile int score;
}
public static void main(String[] args) throws InterruptedException {
tom = new Candidate();
peter = new Candidate();
AtomicIntegerFiledUpdaterDemo demo = new AtomicIntegerFiledUpdaterDemo();
Thread t1 = new Thread(demo);
Thread t2 = new Thread(demo);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("普通变量: " + peter.score);
System.out.println("升级变量: " + tom.score);
}
}
AtomicIntegerFieldUpdater
注意点
◆可见范围
◆不支持 static
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException
at java.base/jdk.internal.misc.Unsafe.objectFieldOffset0(Native Method)
at java.base/jdk.internal.misc.Unsafe.objectFieldOffset(Unsafe.java:955)
at java.base/java.util.concurrent.atomic.AtomicIntegerFieldUpdater$AtomicIntegerFieldUpdaterImpl.<init>(AtomicIntegerFieldUpdater.java:434)
at java.base/java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdater.java:94)
at com.zeny.threadstudy.atomics.AtomicIntegerFiledUpdaterDemo.<clinit>(AtomicIntegerFiledUpdaterDemo.java:12)
◆volatile
声明
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException: Must be volatile type
at java.base/java.util.concurrent.atomic.AtomicIntegerFieldUpdater$AtomicIntegerFieldUpdaterImpl.<init>(AtomicIntegerFieldUpdater.java:420)
at java.base/java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdater.java:94)
at com.zeny.threadstudy.atomics.AtomicIntegerFiledUpdaterDemo.<clinit>(AtomicIntegerFiledUpdaterDemo.java:12)
Adder累加器
◆是Java8
引入的,相对是比较新的一个类
◆高并发下 LongAdder
比 AtomicLong
效率高,不过本质是空间换时间
◆竟争激烈的时候, LongAdder
把不同线程对应到不同的cell
上进行修改,降低了冲突的概率,是多段锁的理念,提高了并发性
◆代码演示
public class AtomicLongDemo {
public static void main(String[] args) {
test();
}
public static void test() {
new Thread(() -> testAtomicLong()).start();
new Thread(() -> testLongAddr()).start();
}
public static void testAtomicLong() {
ExecutorService service = Executors.newFixedThreadPool(20);
AtomicLong counter = new AtomicLong(0);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
service.submit(new TaskAtomicLong(counter));
}
service.shutdown();
while (!service.isTerminated());
long endTime = System.currentTimeMillis();
System.out.println("AtomicLong result: " + counter.get() + ", time: " + (endTime - startTime) + " ms");
}
public static void testLongAddr() {
ExecutorService service = Executors.newFixedThreadPool(20);
LongAdder counter = new LongAdder();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
service.submit(new TaskLongAddr(counter));
}
service.shutdown();
while (!service.isTerminated());
long endTime = System.currentTimeMillis();
System.out.println("LongAddr result: " + counter.sum() + ", time: " + (endTime - startTime) + " ms");
}
private static class TaskAtomicLong implements Runnable {
private AtomicLong counter;
public TaskAtomicLong(AtomicLong counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
counter.incrementAndGet();
}
}
}
private static class TaskLongAddr implements Runnable {
private LongAdder counter;
public TaskLongAddr(LongAdder counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
counter.increment();
}
}
}
}
◆这里演示多线程情况下 AtomicLong
的性能,有20个线程对同个 AtomicLong
累加
◆由于竞争很激烈,每一次加法,AtomicLong
都要flush
和 refresh
,导致很耗费资源。
比较LongAdder
和AtomicLong
的原理:
- 在内部,这个
LongAdder
的实现原理和AtomicLong
是有不同的,AtomicLong
的实现原理是,每一次加法都需要做同步,所以在高并发的时候会导致冲突比较多,也就降低了效率 - 而此时的
LongAdder
,每个线程会有自己的一个计数器,仅用来在自己线程内计数,这样一来就不会和其他线程的计数器干扰,他们之间并不存在竞争关系所以在加和的过程中,根本不需要同步机制,也不需要flush
和Refresh
,这里也没有—个公共的counter
来给所有线程统一计数
LongAddr为啥快??:
LongAdder
引入了分段累加的概念,内部有一个base
变量和一个Cell[]
数组共同参与计数base
变量:竞争不激烈,直接累加到该变量上cell[]
数组:竞争激烈,各个线程分散累加到自己的槽Cell[]
中
public long sum() {
Cell[] cs = cells;
long sum = base;
if (cs != null) {
for (Cell c : cs)
if (c != null)
sum += c.value;
}
return sum;
}
对比 AtomicLong
和 LongAdder
的使用场景:
- 在低争用下,
AtomicLong
和LongAdder
,这两个类具有相似的特征。但是在竞争激烈的情况下,LongAdder
的预期吞吐量要高得多,但要消耗更多的空间 LongAdder
适合的场景是统计求和计数的场景,而且LongAdder
基本只提供了add方法,而AtomicLong
还具有Cas
方法
Accumulator累加器
Accumulator
和 Adder
非常相似, Accumulator
就是一个更通用版本的 Adder
适合大量计算和并行计算, 无先后逻辑顺序.
计算1+2 +3 +...+8+9
的和
LongAccumulator accumulator = new LongAccumulator(Long::sum, 0);
ExecutorService service = Executors.newFixedThreadPool(8);
IntStream.range(1, 10).forEach( i -> service.submit(() -> accumulator.accumulate(i)));
service.shutdown();
while (!service.isTerminated());
System.out.println(accumulator.getThenReset());
计算9!
public static void main(String[] args) {
LongAccumulator accumulator = new LongAccumulator((x ,y) -> x * y, 1);
ExecutorService service = Executors.newFixedThreadPool(8);
IntStream.range(1, 10).forEach( i -> service.submit(() -> accumulator.accumulate(i)));
service.shutdown();
while (!service.isTerminated());
System.out.println(accumulator.getThenReset());
}