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


    一、多线程List集合不安全

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

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

    1.2、解决方法

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

    二、多线程HashSet集合不安全

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

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

    2.2、解决方法

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

    三、多线程HashMap集合不安全

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

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

    3.2、解决方法

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

  • 相关阅读:
    arcgis js 4.x加载超图发布的wms服务并进行空间属性查询
    银行排号系统的设计与实现
    MySQL 索引测试
    前端--第一个前端程序
    使用 etcdadm 快速、弹性部署 etcd 集群
    网络必知题目
    当鼠标移动到table的时候行列都有样式
    服务器多线机房怎么判断
    一文看懂大数据生态圈完整知识体系【大数据技术及架构图解实战派】
    学生作业-购物网站-视频网站-仿百度网站-迪士尼网站-游戏网站-个人主页-个人介绍-音乐网站(作业源码)
  • 原文地址:https://blog.csdn.net/HelloWorld20161112/article/details/134505044