ArrayList HashMap非线程安全 List和Set线程安全实现方式

众所周知ArrayList是非线程安全的,内部采用Object[]数组实现,提供add、remove等操作。以下我们通过源码分析以下,如何线程不安全。

public class ArrayTest {

    public static void main(String[] args) {
        List list = new ArrayList();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                list.add(1);
            }).start();
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        list.stream().forEach(System.out::print);
        System.out.println();

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                list.add(1);
            }).start();
            System.out.println(list);
        }
    }

}

运行结果

null111111111
[null, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[null, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[null, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[null, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[null, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[null, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
	at java.util.ArrayList$Itr.next(ArrayList.java:859)
	at java.util.AbstractCollection.toString(AbstractCollection.java:461)
	at java.lang.String.valueOf(String.java:2994)
	at java.io.PrintStream.println(PrintStream.java:821)
	at com.test.demo.collection.ArrayTest.main(ArrayTest.java:27)

分析一下ArrayList添加元素过程

public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

private void ensureCapacityInternal(int minCapacity) {
    ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}

private static int calculateCapacity(Object[] elementData, int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { // 初始化默认长度为10
        return Math.max(DEFAULT_CAPACITY, minCapacity);
    }
    return minCapacity;
}

private void ensureExplicitCapacity(int minCapacity) {
    modCount++; // 操作数统计+1 重点!!!

    // overflow-conscious code
    if (minCapacity - elementData.length > 0) // 空间不足时 扩容
        grow(minCapacity);
}

private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1); // 扩容为原list的1.5倍
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity); // 新建数组并copy
}


final void checkForComodification() {
    if(modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

在程序进行遍历数组时,调用Itr类中的next方法,会判断modCount != expectedModCount是否成立,如果成立则异常提示。在对list进行修改操作时, 会将modCount +1,循环时发现值被改变, 报出ConcurrentModificationException。

实现线程安全的List有3中方式:1 Vector; 2 Collections.synchronizedList(); 3 CopyOnWriteArrayList

Vector是在所有的方法上都加了同步synchronized关键字, 性能低下,不建议使用

public synchronized E get(int index) {
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
    return elementData(index);
}

**Collections.synchronizedList()**是通过内部类SynchronizedList实现的同步,对元素进行操作前,会使用synchronized对父类中属性mutex加锁,源码:

public E get(int index) {
    synchronized (mutex) {return list.get(index);}
}
public void add(int index, E element) {
    synchronized (mutex) {list.add(index, element);}
}

CopyOnWriteArrayList是JUC包下的并发操作类,核心思想就是在写时使用lock加锁,将原数组copy成新的数组(长度+1),并将值加入新的数组,最后将新的数组赋值给CopyOnWriteArrayList.array,使用volatile修饰,保证可见性。另外,get操作不加锁,如果读写并发,则从旧数组中获取元素。

final transient ReentrantLock lock = new ReentrantLock();
private transient volatile Object[] array;

public boolean add(E e) {
   final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] elements = getArray();
        int len = elements.length;
        Object[] newElements = Arrays.copyOf(elements, len + 1);
        newElements[len] = e;
        setArray(newElements);
        return true;
    } finally {
        lock.unlock();
    }
}

public E get(int index) {
    return get(getArray(), index);
}

HashSet也是非线程安全的,底层实现方式是HashMap。对应线程安全方式有2种:1 Collections.synchronizedSet(); 2 CopyOnWriteSet
Collections.synchronizedSet()的实现方式和Collections.synchronizedList()一致,继承父类SynchronizedCollection,通过synchronized关键字对mutex属性加锁。
CopyOnWriteSet底层实现方式是CopyOnWriteArrayList,添加元素时,判断元素是否已经存在list中,源码如下:

public class CopyOnWriteArraySet<E> extends AbstractSet<E>
        implements java.io.Serializable {
    private static final long serialVersionUID = 5457747651344034263L;

    private final CopyOnWriteArrayList<E> al;

	public CopyOnWriteArraySet() {
        al = new CopyOnWriteArrayList<E>();
    }
	
	public boolean add(E e) {
        return al.addIfAbsent(e);
    }
    
    public boolean addIfAbsent(E e) {
        Object[] snapshot = getArray();
        return indexOf(e, snapshot, 0, snapshot.length) >= 0 ? false :
            addIfAbsent(e, snapshot);
    }

	private boolean addIfAbsent(E e, Object[] snapshot) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] current = getArray();
            int len = current.length;
            if (snapshot != current) {
                // Optimize for lost race to another addXXX operation
                int common = Math.min(snapshot.length, len);
                for (int i = 0; i < common; i++)
                    if (current[i] != snapshot[i] && eq(e, current[i]))
                        return false;
                if (indexOf(e, current, common, len) >= 0)
                        return false;
            }
            Object[] newElements = Arrays.copyOf(current, len + 1);
            newElements[len] = e;
            setArray(newElements);
            return true;
        } finally {
            lock.unlock();
        }
    }
	...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值