# 46. 高并发数据结构选择分析
# 标准答案
✅ 高并发数据结构选择策略:
Map类数据结构:
- ConcurrentHashMap:高并发读写
- ConcurrentSkipListMap:有序场景
- Collections.synchronizedMap:低并发场景
- HashMap + ReadWriteLock:读多写少
List类数据结构:
- CopyOnWriteArrayList:读多写少
- ArrayList + ReadWriteLock:中等并发
- Vector:低并发场景
- LinkedList + Lock:需要频繁插入删除
Queue类数据结构:
- ConcurrentLinkedQueue:无界队列
- ArrayBlockingQueue:有界队列
- LinkedBlockingQueue:生产消费
- PriorityBlockingQueue:优先级队列
# 答案解析
# 1️⃣ 场景选择实现
public class ConcurrentStructureExample {
// 读多写少场景
private final CopyOnWriteArrayList<String> cowList = new CopyOnWriteArrayList<>();
// 高并发读写场景
private final ConcurrentHashMap<String, Object> chm = new ConcurrentHashMap<>();
// 有序Map场景
private final ConcurrentSkipListMap<String, Object> cslm = new ConcurrentSkipListMap<>();
// 生产消费场景
private final BlockingQueue<Task> taskQueue = new LinkedBlockingQueue<>(1000);
public void handleRequest(Request request) {
switch(request.getType()) {
case READ_HEAVY:
// 读多写少,使用CopyOnWrite
cowList.add(request.getData());
break;
case CONCURRENT_ACCESS:
// 高并发读写,使用ConcurrentHashMap
chm.put(request.getKey(), request.getValue());
break;
case ORDERED_ACCESS:
// 需要排序,使用ConcurrentSkipListMap
cslm.put(request.getKey(), request.getValue());
break;
case PRODUCER_CONSUMER:
// 生产消费模式,使用BlockingQueue
taskQueue.offer(new Task(request));
break;
}
}
}
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
33
34
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
33
34
# 2️⃣ 性能优化实现
public class OptimizedCollections {
// 分段锁实现
private final Map<String, Object>[] segments;
private final int mask;
public OptimizedCollections(int segmentCount) {
// 确保是2的幂
int count = 1;
while (count < segmentCount) {
count <<= 1;
}
segments = new ConcurrentHashMap[count];
for (int i = 0; i < count; i++) {
segments[i] = new ConcurrentHashMap<>();
}
mask = count - 1;
}
public Object get(String key) {
return getSegment(key).get(key);
}
public void put(String key, Object value) {
getSegment(key).put(key, value);
}
private Map<String, Object> getSegment(String key) {
return segments[key.hashCode() & mask];
}
}
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
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
# 常见误区
- ❌ 误区1:盲目使用同步集合类
- ❌ 误区2:忽视读写比例的考虑
# 典型场景与解决方案
# ✅ 缓存实现
public class ConcurrentCache<K, V> {
private final ConcurrentHashMap<K, V> cache = new ConcurrentHashMap<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
public V get(K key) {
V value = cache.get(key);
if (value == null) {
lock.writeLock().lock();
try {
value = cache.get(key);
if (value == null) {
value = loadValue(key);
cache.put(key, value);
}
} finally {
lock.writeLock().unlock();
}
}
return value;
}
private V loadValue(K key) {
// 从数据源加载值
return null;
}
}
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
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
# 企业实战经验
# Situation(业务背景)
电商系统商品目录需要高并发读写。
# Task(核心任务)
选择合适的数据结构实现高性能缓存。
# Action(解决方案)
- 分析读写比例
- 评估数据量大小
- 考虑内存占用
- 测试不同方案
# Result(结果)
- 读取性能提升200%
- 内存占用降低30%
- 系统稳定性提升
# 深入追问
🔹 如何权衡并发度和内存占用?
- 评估实际并发量
- 监控内存使用
- 进行压力测试
🔹 在读多写少场景中,如何优化性能?
- 使用读写分离
- 实现多级缓存
- 采用Copy-On-Write
# 相关面试题
- ConcurrentHashMap的实现原理?
- CopyOnWriteArrayList适用场景?
- 不同队列实现的性能对比?