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

一、多线程List集合不安全

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

java 复制代码
/**
 * @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、解决方法

java 复制代码
/**
 * @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集合不安全案例代码

java 复制代码
/**
 * @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、解决方法

java 复制代码
/**
 * @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集合不安全案例代码

java 复制代码
/**
 * @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、解决方法

java 复制代码
/**
 * @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();
        }
    }

}
相关推荐
阿维的博客日记23 天前
用volatile修饰数组代表什么意思,Java
java·juc·volatile
是三好1 个月前
并发容器(Collections)
java·多线程·juc
编程、小哥哥1 个月前
互联网大厂Java求职面试实录 —— 严肃面试官遇到搞笑水货程序员
java·面试·mybatis·dubbo·springboot·多线程·juc
yb0os11 个月前
手写一个简单的线程池
java·开发语言·数据库·计算机·线程池·juc
是三好1 个月前
Lock锁
java·juc
abc小陈先生1 个月前
JUC并发编程1
java·juc
左灯右行的爱情2 个月前
深入理解 G1 GC:已记忆集合(RSet)与收集集合(CSet)详解
java·jvm·后端·juc
左灯右行的爱情3 个月前
深入学习ReentrantLock
java·后端·juc
佛祖让我来巡山4 个月前
JUC相关知识点总结
juc
fly spider5 个月前
多线程-线程池的使用
java·面试·线程池·多线程·juc