1、concurrent包下常用的线程安全的集合
CopyOnWriteArrayList
package com.company.ThreadTest;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class TestJUC_CopyOnWriteArrayList {
public static void main(String[] args) {
//并发下ArrayList是不安全的可能会报异常:ConcurrentModificationException
/**
* 解决方案:
* 1、List<String> list = new Vector<>();
* 2、List<String> list = Collections.synchronizedList(new ArrayList<>());
* 3、List<String> list = new CopyOnWriteArrayList<>();
*/
//CopyOnWrite 写入时复制 COW思想:计算机设计领域的一种优化策略
//多线程调用时避免写入覆盖,写入之前先复制一份
List<String> list = new CopyOnWriteArrayList<>();
for (int i = 0; i < 10; i++) {
new Thread(()->{
list.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(list);
}).start();
}
}
}
结果:
[c316f]
[c316f, 350ba, 6b5a7, c1f25, 3a7b6]
[c316f, 350ba, 6b5a7, c1f25]
[c316f, 350ba, 6b5a7]
[c316f, 350ba, 6b5a7, c1f25, 3a7b6, f7f18]
[c316f, 350ba]
[c316f, 350ba, 6b5a7, c1f25, 3a7b6, f7f18, c4f02]
[c316f, 350ba, 6b5a7, c1f25, 3a7b6, f7f18, c4f02, 8aca3, c9f3c]
[c316f, 350ba, 6b5a7, c1f25, 3a7b6, f7f18, c4f02, 8aca3]
[c316f, 350ba, 6b5a7, c1f25, 3a7b6, f7f18, c4f02, 8aca3, c9f3c, d459e]
CopyOnWriteArraySet
package com.company.ThreadTest;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
public class TestJUC_CopyOnWriteArraySet {
public static void main(String[] args) {
//并发下HashSet是不安全的可能会报异常:ConcurrentModificationException
/**
* 解决方案:
* 1、Set<String> set = Collections.synchronizedSet(new HashSet<>());
* 2、Set<String> set = new CopyOnWriteArraySet<>();
*/
Set<String> set = new CopyOnWriteArraySet<>();
for (int i = 0; i < 10; i++) {
new Thread(()->{
set.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(set);
}).start();
}
}
}
结果:
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727, 8d260]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727, 8d260]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727, 8d260]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727, 8d260]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727]
[41cc1, ca30b, d90e0, 480d5, ec67b, f5dfa, c7494, 6d727, 8d260, e6dca]
ConcurrentHashMap
package com.company.ThreadTest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class TestJUC_ConcurrentHashMap {
public static void main(String[] args) {
//并发下HashMap是不安全的可能会报异常:ConcurrentModificationException
/**
* 解决方案:
* 1、Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
* 2、Map<String,String> map = new ConcurrentHashMap<>();
*/
Map<String,String> map = new ConcurrentHashMap<>();
for (int i = 0; i < 10; i++) {
new Thread(()->{
map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,5));
System.out.println(map);
}).start();
}
}
}
结果:
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-8=8fef8, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-8=8fef8, Thread-9=db25d, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-8=8fef8, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-8=8fef8, Thread-9=db25d, Thread-0=05ea8, Thread-2=62441}
{Thread-3=72d8c, Thread-4=2d5d0, Thread-5=a2291, Thread-6=c2cca, Thread-7=1f50f, Thread-8=8fef8, Thread-9=db25d, Thread-0=05ea8, Thread-1=95c99, Thread-2=62441}
ConcurrentHashMap原理:
网友评论