当前位置: 首页 > news >正文

系列六、多线程集合不安全

一、多线程List集合不安全

1.1、List集合不安全案例代码

/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 12:38* @Description: 多线层环境下List集合不安全案例代码*/
public class NotSafeListMainApp {public static void main(String[] args) {List<String> list = new ArrayList<>();for (int i = 1; i <= 30; i++) {new Thread(() -> {list.add(UUID.randomUUID().toString().toLowerCase().substring(0, 8).replaceAll("-", ""));System.out.println("【当前线程】:" + Thread.currentThread().getName() + ",【当前list】:" + list);}, String.valueOf(i)).start();}}}

1.2、解决方法

/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 12:38* @Description: 多线层环境下List集合不安全解决案例代码*/
public class NotSafeListPlusMainApp {public static void main(String[] args) {// List<String> list = Collections.synchronizedList(new ArrayList<>());List<String> list = new CopyOnWriteArrayList<>();for (int i = 1; i <= 30; i++) {new Thread(() -> {list.add(UUID.randomUUID().toString().toLowerCase().substring(0, 8).replaceAll("-", ""));System.out.println("【当前线程】:" + Thread.currentThread().getName() + ",【当前list】:" + list);}, String.valueOf(i)).start();}}}

二、多线程HashSet集合不安全

2.1、HashSet集合不安全案例代码

/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 12:38* @Description: 多线层环境下HashSet集合不安全案例代码*/
public class NotSafeHashSetMainApp {public static void main(String[] args) {Set<String> hashSet = new HashSet<>();for (int i = 1; i <= 30; i++) {new Thread(() -> {hashSet.add(UUID.randomUUID().toString().toLowerCase().substring(0, 8).replaceAll("-", ""));System.out.println("【当前线程】:" + Thread.currentThread().getName() + ",【当前hashSet】:" + hashSet);}, String.valueOf(i)).start();}}}

2.2、解决方法

/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 12:38* @Description: 多线层环境下HashSet集合不安全解决案例代码*/
public class NotSafeHashSetPlusMainApp {public static void main(String[] args) {// Set<String> hashSet = Collections.synchronizedSet(new HashSet<>());Set<String> hashSet = new CopyOnWriteArraySet<>();for (int i = 1; i <= 30; i++) {new Thread(() -> {hashSet.add(UUID.randomUUID().toString().toLowerCase().substring(0, 8).replaceAll("-", ""));System.out.println("【当前线程】:" + Thread.currentThread().getName() + ",【当前hashSet】:" + hashSet);}, String.valueOf(i)).start();}}}

三、多线程HashMap集合不安全

3.1、HashMap集合不安全案例代码

/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 12:38* @Description: 多线层环境下HashMap集合不安全案例代码*/
public class NotSafeHashMapMainApp {public static void main(String[] args) {HashMap<String,Object> hashMap = new HashMap<>();for (int i = 1; i <= 30; i++) {new Thread(() -> {hashMap.put(UUID.randomUUID().toString().substring(0, 1),UUID.randomUUID().toString().toLowerCase().substring(0, 8).replaceAll("-", ""));System.out.println("【当前线程】:" + Thread.currentThread().getName() + ",【当前hashMap】:" + hashMap);}, String.valueOf(i)).start();}}}

3.2、解决方法

/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 12:38* @Description: 多线层环境下HashMap集合不安全解决案例代码*/
public class NotSafeHashMapPlusMainApp {public static void main(String[] args) {// Map<String,Object> hashMap = Collections.synchronizedMap(new HashMap<>());Map<String,Object> hashMap = new ConcurrentHashMap<>();for (int i = 1; i <= 30; i++) {new Thread(() -> {hashMap.put(UUID.randomUUID().toString().substring(0, 1),UUID.randomUUID().toString().toLowerCase().substring(0, 8).replaceAll("-", ""));System.out.println("【当前线程】:" + Thread.currentThread().getName() + ",【当前hashMap】:" + hashMap);}, String.valueOf(i)).start();}}}

http://www.lryc.cn/news/236682.html

相关文章:

  • MidJourney笔记(1)-入门
  • CRM系统定制开发价格
  • Kubernetes实战(五)-pod之间网络请求实战
  • 7年经验之谈 —— 如何高效的开展app的性能测试?
  • 小程序action-sheet结合自定义tabbar显示
  • 机器学习笔记 - 隐马尔可夫模型的简述
  • iOS学习 --- Xcode 15 下载iOS_17.0.1_Simulator失败解决方法
  • 多视图聚类论文阅读(二)
  • Docker在Centos7下的安装
  • LLM大模型4位量化实战【GPTQ】
  • 安装keras、tensorflow
  • ffmpeg知识点整理
  • Git 笔记之gitignore
  • 【配置】Redis常用配置详解
  • Linux(Ubuntu)安装JDK环境
  • OpenCV C++ 张正友相机标定【相机标定原理、相机标定流程、图像畸变矫正】
  • SDL2 播放音频(MP4)
  • WMS仓库管理系统库位功能
  • vue2组件通信中的一些拓展(props,emit,ref父子双向传参)
  • Flink1.17 DataStream API
  • 数据结构中树、森林 与 二叉树的转换
  • 力扣labuladong——一刷day43
  • MapApp 地图应用
  • Java之反射获取和赋值字段
  • ckplayer自己定义风格播放器的开发记录
  • 全网最全Django面试题整理(一)
  • vue统一登录
  • MVSNet论文笔记
  • 大型 APP 的性能优化思路
  • K8S配置资源管理