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

news/2024/5/20 10:19:54 标签: JUC

一、多线程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.niftyadmin.cn/n/5196805.html

相关文章

js高效函数库Lodash.js

Lodash 是一个 JavaScript 的实用工具库&#xff0c;提供了许多实用且高效的函数&#xff0c;可以简化 JavaScript 编程中的常见任务。 Lodash具有高性能、模块化和易用性等特点&#xff0c;表现一致性以及可扩展&#xff0c;下面将介绍一些 Lodash 的重要特性和用法&#xff1…

开发《星球大战》小游戏的意义

开发《星球大战》小游戏的意义有以下几点&#xff1a; 学习和掌握游戏开发的基本技能&#xff1a;通过开发《星球大战》小游戏&#xff0c;可以学习和掌握游戏开发的基本技能&#xff0c;包括游戏策划、游戏设计、游戏编程和游戏测试等方面的技能。加深对游戏行业的了解&…

根据音频绘制频谱

根据音频链接绘制频谱图 封装 // 可以这样使用 也可以 import { AudioContext } from standardized-audio-context; const getAudioContext window.AudioContext ||window.webkitAudioContext ||window.mozAudioContext ||window.msAudioContext;const clearArr []export c…

音频录制实现 绘制频谱

思路 获取设备信息 获取录音的频谱数据 绘制频谱图 具体实现 封装 loadDevices.js /*** 是否支持录音*/ const recordingSupport () > {const scope navigator.mediaDevices || {};if (!scope.getUserMedia) {scope navigatorscope.getUserMedia || (scope.getUserM…

SSL证书对网站SEO的好处

随着网络安全意识的提高&#xff0c;越来越多的网站开始采用SSL证书来保护自己的数据传输过程。那么&#xff0c;SSL证书真的能为网站SEO带来好处吗&#xff1f;下面将为您分析这个问题。 加强用户体验和信任度 SSL证书不仅能确保数据传输的安全性&#xff0c;还能让客户感受…

【shell】 1、bash语法超详细介绍

文章目录 修改前缀路径dirname set常用函数参数变量local 返回值正则打印第 n 行获取行号核对数据库各表数量jq查询检查日志 sshpassexpect数组xargs bash manual 修改前缀 参考 export PS1"bash> "路径 dirname strip last component from file name dir$(…

使用JDK自带java.util.logging.Logger引起的冲突问题

现象&#xff1a; 应用代码如下&#xff1a; import javax.script.ScriptEngineManager;ScriptEngineManager manager new ScriptEngineManager(); manager.getEngineByName("JavaScript"); 在TongWeb8上运行出错&#xff0c;日志如下&#xff1a; Servlet.servi…

c++ call_once 使用详解

c call_once 使用详解 std::call_once 头文件 #include <mutex>。 函数原型&#xff1a; template<class Callable, class... Args> void call_once(std::once_flag& flag, Callable&& f, Args&&... args);flag&#xff1a;标志对象&#xf…