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


    一、多线程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. }

  • 相关阅读:
    计算机毕业设计springboot+vue基本微信小程序的学习资料共享交流小程序
    【IDEA】idea恢复pom.xml文件显示灰色并带有删除线
    QT tableview 实现千万数据加载,内存消耗小
    ByteV联合“智农”打造--数字孪生大棚可视化
    202.快乐数
    遥感典型任务分析
    【Linux从青铜到王者】 基础IO
    K8s 里如何优雅地使用 /dev/shm 实现容器间共享内存
    springboot+vue+Elementui共享单车管理系统
    leetcode-136. 只出现一次的数字
  • 原文地址:https://blog.csdn.net/HelloWorld20161112/article/details/134505044