ArrayList线程不安全原因、复现以及解决办法

原因

因为他的读写方法没有同步策略,会导致脏数据和不可预期的结果。

// 非原子方法,并且没有同步策略
public class ArrayList<E> 
{

    public E get(int index) {
        rangeCheck(index);
        return elementData(index);
    }

    public E set(int index, E element) {
        rangeCheck(index);
        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }
}

复现

package com.wenxiaowu.concurrent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 复现ArrayList线程不安全问题
 */
public class ArrayList1Test {
    public static void main(String[] args) {
        final List<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(() -> {
                Thread currentThread = Thread.currentThread();
                String threadName = currentThread.getName();

                for (int j = 0; j < 5; j++) {
                    String nowTime = getNowTime();
                    arrayList.add(String.format("%d", j));
                    System.out.println(String.format("%s %s, list size:%d, detail:%s",
                            nowTime, threadName, arrayList.size(), arrayList.toString()));
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
            });
            thread.setName(String.format("thread%d", i));
            thread.start();
        }
    }

    private static String getNowTime() {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return df.format(day);
    }
}

出现了经典的并发修改问题
注:有的时候需要多试几次才能复现。

解决办法

final List<String> arrayList = new ArrayList<>();
改成
final List<String> arrayList = new CopyOnWriteArrayList<String>();
即可

底层解析:CopyOnWriteArrayList存储数据的数组是volatile修饰的,线程可见,禁止重排。在高并发场景里面,重要的核心变量都需要加volatile。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文晓武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值