站点图标 度崩网-几度崩溃

Java中有哪些无锁技术来解决并发问题?如何使用?

除了使用 synchronized、Lock 加锁之外,Java 中还有很多不需要加锁就可以解决并发问题的工具类

1、原子工具类

JDK 1.8 中,java.util.concurrent.atomic 包下类都是原子类,原子类都是基于sun.misc.Unsafe 实现的。

原子性基本数据类型:AtomicBoolean、AtomicInteger、AtomicLong

原子性对象引用类型:AtomicReference、AtomicStampedReference、AtomicMarkableReference

原子性数组:AtomicIntegerArray、AtomicLongArray、AtomicReferenceArray

原子性对象属性更新:AtomicIntegerFieldUpdater、AtomicLongFieldUpdater、AtomicReferenceFieldUpdater

原子性累加器:DoubleAccumulator、DoubleAdder、LongAccumulator、LongAdder

修改我们之前测试原子性问题的类,使用 AtomicInteger 的简单例子

package constxiong.concurrency.a026;import java.util.concurrent.atomic.AtomicInteger;/** * 测试 原子类 AtomicInteger *  * @author ConstXiong */public class TestAtomicInteger {// 计数变量static volatile AtomicInteger count = new AtomicInteger(0);public static void main(String[] args) throws InterruptedException {// 线程 1 给 count 加 10000Thread t1 = new Thread(() -> {for (int j = 0; j <10000; j++) {count.incrementAndGet();}System.out.println("thread t1 count 加 10000 结束");});// 线程 2 给 count 加 10000Thread t2 = new Thread(() -> {for (int j = 0; j <10000; j++) {count.incrementAndGet();}System.out.println("thread t2 count 加 10000 结束");});// 启动线程 1t1.start();// 启动线程 2t2.start();// 等待线程 1 执行完成t1.join();// 等待线程 2 执行完成t2.join();// 打印 count 变量System.out.println(count.get());}}

打印结果如预期

thread t2 count 加 10000 结束thread t1 count 加 10000 结束20000

2、线程本地存储

示例

package constxiong.concurrency.a026;/** * 测试 原子类 AtomicInteger *  * @author ConstXiong */public class TestThreadLocal {// 线程本地存储变量private static final ThreadLocal<Integer> THREAD_LOCAL_NUM = new ThreadLocal<Integer>() {@Overrideprotected Integer initialValue() {//初始值return 0;}};public static void main(String[] args) {for (int i = 0; i <3; i++) {// 启动三个线程Thread t = new Thread() {@Overridepublic void run() {add10ByThreadLocal();}};t.start();}}/** * 线程本地存储变量加 5 */private static void add10ByThreadLocal() {try {for (int i = 0; i <5; i++) {Integer n = THREAD_LOCAL_NUM.get();n += 1;THREAD_LOCAL_NUM.set(n);System.out.println(Thread.currentThread().getName() + " : ThreadLocal num=" + n);}} finally {THREAD_LOCAL_NUM.remove();// 将变量移除}}}

每个线程最后一个值都打印到了 5

Thread-0 : ThreadLocal num=1Thread-2 : ThreadLocal num=1Thread-1 : ThreadLocal num=1Thread-2 : ThreadLocal num=2Thread-0 : ThreadLocal num=2Thread-2 : ThreadLocal num=3Thread-0 : ThreadLocal num=3Thread-1 : ThreadLocal num=2Thread-0 : ThreadLocal num=4Thread-2 : ThreadLocal num=4Thread-0 : ThreadLocal num=5Thread-1 : ThreadLocal num=3Thread-2 : ThreadLocal num=5Thread-1 : ThreadLocal num=4Thread-1 : ThreadLocal num=5

3、copy-on-write

简单的CopyOnWriteArrayList 的示例,这里只是说明CopyOnWriteArrayList 怎么用,并且是线程安全的。这个场景并不适合使用CopyOnWriteArrayList,因为写多读少。

package constxiong.concurrency.a026;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.concurrent.CopyOnWriteArrayList;/** * 测试 copy-on-write * @author ConstXiong */public class TestCopyOnWrite {private static final Random R = new Random();private static CopyOnWriteArrayList<Integer> cowList = new CopyOnWriteArrayList<Integer>();//private static ArrayList<Integer> cowList = new ArrayList<Integer>();public static void main(String[] args) throws InterruptedException {List<Thread> threadList = new ArrayList<Thread>();//启动 1000 个线程,向 cowList 添加 5 个随机整数for (int i = 0; i <1000; i++) {Thread t = new Thread(() -> {for (int j = 0; j <5; j++) {//休眠 10 毫秒,让线程同时向 cowList 添加整数,引出并发问题try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}cowList.add(R.nextInt(100));}}) ;t.start();threadList.add(t);}for (Thread t : threadList) {t.join();}System.out.println(cowList.size());}}

打印结果

5000

如果把

private static CopyOnWriteArrayList<Integer> cowList = new CopyOnWriteArrayList<Integer>();

改为

private static ArrayList<Integer> cowList = new ArrayList<Integer>();

打印结果就是小于 5000 的整数了

4、其他 "Concurrent" 开头的并发工具类,如:ConcurrentHashMap、ConcurrentLinkedDeque、ConcurrentLinkedQueue…