0%

集合类线程不安全问题

ArrayList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @author xixing
* @version 1.0
* @date 2020/7/2 14:53
*/
public class ContainerNotSafeDemo {

public static void main(String[] args) {
List<String> list= new CopyOnWriteArrayList<>();
for(int i=0;i<100;i++){
new Thread(()->{
list.add(UUID.randomUUID().toString().substring(0,8));
System.out.println(list);
},String.valueOf(i)).start();
}
//java.util.ConcurrentModificationException

/**
*1.故障现象
* java.util.ConcurrentModificationException
*
*2.导致原因
* 并发争抢修改导致,参考花名册签名,一个正在写,另一个抢导致
*3.解决方案
* 1.new Vector<>() 不建议,这个类已经被不建议使用。
* 2.Collections.synchronizedList(new ArrayList())
* 3.new CopyOnWriteArrayList<>();
*
*
*/
}
}

image-20200702152923248

HashSet

HashSet底层是HashMap,以key为set,value是一个object的常量。

1
2
3
4
5
6
7
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Adds the specified element to this set if it is not already present.
* More formally, adds the specified element <tt>e</tt> to this set if
* this set contains no element <tt>e2</tt> such that
* <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
* unchanged and returns <tt>false</tt>.
*
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
1
2
3
4
5
6
7
Set<String> set= new CopyOnWriteArraySet<>();
for(int i=0;i<100;i++){
new Thread(()->{
set.add(UUID.randomUUID().toString().substring(0,8));
System.out.println(set);
},String.valueOf(i)).start();
}

HashMap

1
2
3
4
5
6
7
Map<String,String> map= new ConcurrentHashMap<>();
for(int i=0;i<100;i++){
new Thread(()->{
map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,8));
System.out.println(map);
},String.valueOf(i)).start();
}