- JDK Atomic beginning of the class is to solve the problem of concurrency, atomicity principle by CAS
- 3 parameters CAS, CAS (V, E, N). V represents a variable to be updated, E represents the expected value of the current variable, N denotes the updated value. Only when the value of the variable V is equal to E, the value will be updated variable V is N. If the variable is not equal to the value of V E, explained the value of the variable V has been updated, the current thread does nothing and returns the update failure
- When multiple threads simultaneously using CAS update a variable, only one thread can update is successful, other have failed. The failure of the thread is not suspended, it can continue to retry CAS, you can also abort the operation
- Atomic CAS operation is ensured by the CPU to complete a single instruction. Unsafe by the JDK API class completed
- In the case of high concurrency, there will be a large number of CAS and retry the update fails, it is necessary to caution
- java.util.concurrent.atomic package into atoms: atoms of the basic data types of the object reference type atom, an array of atoms, atomic object attributes and updating atomic accumulator
原子性基本数据类型:AtomicBoolean、AtomicInteger、AtomicLong
原子性对象引用类型:AtomicReference、AtomicStampedReference、AtomicMarkableReference
原子性数组:AtomicIntegerArray、AtomicLongArray、AtomicReferenceArray
原子性对象属性更新:AtomicIntegerFieldUpdater、AtomicLongFieldUpdater、AtomicReferenceFieldUpdater
原子性累加器:DoubleAccumulator、DoubleAdder、LongAccumulator、LongAdder
Unused atoms class, test code
package constxiong.interview;
/**
* JDK 原子类测试
* @author ConstXiong
*/
public class TestAtomic {
private int count = 0;
public int getAndIncrement() {
return count++;
}
// private AtomicInteger count = new AtomicInteger(0);
//
// public int getAndIncrement() {
// return count.getAndIncrement();
// }
public static void main(String[] args) {
final TestAtomic test = new TestAtomic();
for (int i = 0; i <3; i++) {
new Thread(){
@Override
public void run() {
for (int j = 0; j <10; j++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 获取递增值:" + test.getAndIncrement());
}
}
}.start();
}
}
}
Print results, contain duplicate values
Thread-0 获取递增值:1
Thread-2 获取递增值:2
Thread-1 获取递增值:0
Thread-0 获取递增值:3
Thread-2 获取递增值:3
Thread-1 获取递增值:3
Thread-2 获取递增值:4
Thread-0 获取递增值:5
Thread-1 获取递增值:5
Thread-1 获取递增值:6
Thread-2 获取递增值:8
Thread-0 获取递增值:7
Thread-1 获取递增值:9
Thread-0 获取递增值:10
Thread-2 获取递增值:10
Thread-0 获取递增值:11
Thread-2 获取递增值:13
Thread-1 获取递增值:12
Thread-1 获取递增值:14
Thread-0 获取递增值:14
Thread-2 获取递增值:14
Thread-1 获取递增值:15
Thread-2 获取递增值:15
Thread-0 获取递增值:16
Thread-1 获取递增值:17
Thread-0 获取递增值:19
Thread-2 获取递增值:18
Thread-0 获取递增值:20
Thread-1 获取递增值:21
Thread-2 获取递增值:22
Test code type modify atoms
package constxiong.interview;
import java.util.concurrent.atomic.AtomicInteger;
/**
* JDK 原子类测试
* @author ConstXiong
* @date 2019-06-11 11:22:01
*/
public class TestAtomic {
// private int count = 0;
//
// public int getAndIncrement() {
// return count++;
// }
private AtomicInteger count = new AtomicInteger(0);
public int getAndIncrement() {
return count.getAndIncrement();
}
public static void main(String[] args) {
final TestAtomic test = new TestAtomic();
for (int i = 0; i <3; i++) {
new Thread(){
@Override
public void run() {
for (int j = 0; j <10; j++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 获取递增值:" + test.getAndIncrement());
}
}
}.start();
}
}
}
The printed result, does not contain duplicate values
Thread-0 获取递增值:1
Thread-2 获取递增值:2
Thread-1 获取递增值:0
Thread-0 获取递增值:3
Thread-1 获取递增值:4
Thread-2 获取递增值:5
Thread-0 获取递增值:6
Thread-1 获取递增值:7
Thread-2 获取递增值:8
Thread-0 获取递增值:9
Thread-2 获取递增值:10
Thread-1 获取递增值:11
Thread-0 获取递增值:12
Thread-1 获取递增值:13
Thread-2 获取递增值:14
Thread-0 获取递增值:15
Thread-1 获取递增值:16
Thread-2 获取递增值:17
Thread-0 获取递增值:18
Thread-1 获取递增值:19
Thread-2 获取递增值:20
Thread-0 获取递增值:21
Thread-2 获取递增值:23
Thread-1 获取递增值:22
Thread-0 获取递增值:24
Thread-1 获取递增值:25
Thread-2 获取递增值:26
Thread-0 获取递增值:27
Thread-2 获取递增值:28
Thread-1 获取递增值:29
- Java self-experience
- Java interview questions H5
- Java applets interview questions