Java集合多线程安全
线程安全与不安全集合
线程不安全集合:
- ArrayList
- LinkedList
- HashMap
- HashSet
- TreeMap
- TreeSet
- StringBulider
线程安全集合:
- Vector
- HashTable
- Properties
集合线程安全与解决方案
ArrayList线程安全问题
package com.raicho.mianshi.mycollection; import java.util.ArrayList; import java.util.List; import java.util.UUID; /** * @author: Raicho * @Description: * @program: mianshi * @create: 2020-07-17 15:32 **/ public class ArrayListConcurrentDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); for (int i = 0; i < 30; ++i) { new Thread(() -> { list.add(UUID.randomUUID().randomUUID().toString().substring(0, 4)); System.out.println(list); }).start(); } } }