集合类多线程不安全问题
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* 集合类不安全问题
* ArrayList
* HashSet ---> 底层HashMap ---> hashset的add方法第二个参数不管 ,用hashmap put了一个new Object()
* HashMap
*/
public class ContainerNotSafeDemo {
public static void main(String[] args) {
// listNotSafe();
// setNotSafe();
mapNotSafe();
}
public static void listNotSafe() {
/**
* new CopyOnWriteArrayList<>();
*/
//注意 Collection是接口 Collections加了s是类
List<String> list = new CopyOnWriteArrayList<>();//Collections.synchronizedList(new ArrayList<>());//new Vector<>();//new ArrayList<>(); //按住List 右键->Diagrams-> show
for (int i=1;i<=30;i++){ //3 30
new Thread(() -> {
list.add(UUID.randomUUID().toString().substring(0, 8));
System.out.println(list);
},String.valueOf(i)).start();
}
}
public static void setNotSafe(){
/**
* CopyOnWriteArraySet<>();
*/
Set<String> set = new CopyOnWriteArraySet<>(); //换汤不换药
for(int i=1;i<=30;i++){
new Thread(()->{
set.add(UUID.randomUUID().toString().substring(0,8));
System.out.println(set);
},String.valueOf(i)).start();
}
new HashSet<>().add("a");
}
public static void mapNotSafe(){
/**
* new ConcurrentHashMap<>();
*/
Map<String, String> map = new ConcurrentHashMap<>();//new HashMap<>();
for(int i=1;i<=30;i++){
new Thread(()->{
map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0, 8));
System.out.println(map);
},String.valueOf(i)).start();
}
}
}
/**
* 以ArrayList为例
*
* 1.故障现象
* java.util.ConcurrentModificationException
*
* 2.导致原因
* 并发修改争抢,参考花名册签名
* 一个人正在写,另一个人暴力抢走,导致并发修改异常
*
* 3.解决方案
* 3.1 new Vector<>();
* 它用了锁,30个完全没问题解决
* 但是 并发性急剧下降
*
* 3.2 Collections.synchronizedList(new ArrayList<>()); 用了工具类
*
* 3.3 new CopyOnWriteArrayList<>();
*
* 4.优化建议(同样的错误不犯两次)
*/
/*
以ArrayList为例
3个线程的时候
正常结果
[cc74e8b5, 8fec3fcb, 67b55002]
[cc74e8b5, 8fec3fcb, 67b55002]
[cc74e8b5, 8fec3fcb, 67b55002]
多运行几遍
[6fb1b189, bf82e74c]
[6fb1b189, bf82e74c, 4ae93dab]
[6fb1b189, bf82e74c]
30个线程的时候
出现报错
java.util.ConcurrentModificationException
高并发多线程常出现的报错 并发修改异常
修改后
[819237d1, 49e81c7c, 2095ba99]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954]
[819237d1, 49e81c7c, 2095ba99, e6600494]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf]
[819237d1, 49e81c7c, 2095ba99]
[819237d1, 49e81c7c, 2095ba99]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25, f1926dba]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25, f1926dba, 8af8f8ad, e4b2dff6]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25, f1926dba, 8af8f8ad, e4b2dff6, aeede716, e02d1b6f, e30d2a23]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25, f1926dba, 8af8f8ad, e4b2dff6, aeede716, e02d1b6f]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25, f1926dba, 8af8f8ad, e4b2dff6, aeede716]
[819237d1, 49e81c7c, 2095ba99, e6600494, d70addee, 8b47b829, 9659c0bb, 6ac53139, 67ca2492, 342ff4dc, e02b0348, 049f9954, dd08924f, 0b9f9517, be0ebfdf, 711a7a86, 49fdfe55, 96f7c126, 7e1a47a8, 23adc2eb, f8fcfc2d, a4a6fe98, a667a5d2, 01cc9d25, f1926dba, 8af8f8ad]
Process finished with exit code 0
*/