当前位置:网站首页>集合和Map线程安全问题解决
集合和Map线程安全问题解决
2022-04-22 05:33:00 【linsa_pursuer】
1.ArrayList线程不安全(没有加可重入锁) 解决方法 Vector(synchronized) Collections(synchronized) CopyOnWriteArrayList(ReentrantLock)
2.HashSet——》CopyOnWriteArraySet
3.HashMapt——》ConcurrentHashMap
package juc.two;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* ArrayList线程不安全(没有加可重入锁)
* 解决方法 Vector(synchronized) Collections(synchronized) CopyOnWriteArrayList(ReentrantLock)
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//创建ArrayList集合
//List<String> list = new ArrayList<>();
//Vector解决
//List<String> list = new Vector<>();
//Collections解决
//List<String> list = Collections.synchronizedList(new ArrayList<>());
//CopyOnWriteArrayList解决
//List<String> list = new CopyOnWriteArrayList<>();
/*for(int i = 0; i<30; i++){
new Thread(()->{
//向集合添加内容
list.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(list);
},String.valueOf(i)).start();
}*/
//演示HashSet
//Set<String> set = new HashSet<>();
//Set<String> set = new CopyOnWriteArraySet<>();
/*for(int i = 0; i<30; i++){
new Thread(()->{
//向集合添加内容
set.add(UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(set);
},String.valueOf(i)).start();
}*/
//演示HashMap
//Map<String,String> map = new HashMap<>();
Map<String,String> map = new ConcurrentHashMap<>();
for(int i = 0; i<30; i++){
String key = String.valueOf(i);
new Thread(()->{
//向集合添加内容
map.put(key,UUID.randomUUID().toString().substring(0,8));
//从集合获取内容
System.out.println(map);
},String.valueOf(i)).start();
}
}
}
版权声明
本文为[linsa_pursuer]所创,转载请带上原文链接,感谢
https://blog.csdn.net/linsa_pursuer/article/details/124310603
边栏推荐
- 可重入锁卖票示例
- Network attack and Defense Security Learning Platform - upload key 3
- GBase 8s V8.8 SQL 指南:教程-5.2(3)
- MySQL数据库基础
- 环形链表2
- 2022.4.21-----leetcode.824
- GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.2 (1)
- Fundamentals of graphics - depth of field / DOF
- fastjson判断JSON字符串是Object还是List<Object>
- [fedmd, a heterogeneous FL training method using model distillation] fedmd: heterogeneous federated learning via model distillation
猜你喜欢
随机推荐
5.The Simple Problem
Fundamentals of graphics | real time shadow rendering
Application of C language stack: binary conversion
2.words平均长度
环形链表2
【转】MySQL:InnoDB一棵B+树可以存放多少行数据?
顺序存储的二叉树的最近的公共祖先问题|PTA
3.连续整数Consecutive Integer
Simulate the infectious disease model with MATLAB (only do matlab simulation learning and practice, not actual situation and application)
2022年JS新增小技巧
MySQL事务
C language practice (2) -- polynomial addition with linked list
MySQL Chapter 7 complex query of data table
Write a program to automatically generate the two-color ball number of welfare lottery with MATLAB
Recommended system notes (Miscellaneous)
Redis实时同步工具推荐
Five important properties of binary tree
The nearest common ancestor problem of sequentially stored binary trees | PTA
GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.1 (4)
GBase 8s V8.8 SQL 指南:教程-6.1.1(4)








