java并发编程 第三期 多线程中的集合

多线程中的集合是不安全的

package com.collection;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxu
 * @date 2021/11/11 22:28
 */
public class ArrayListDemo {
    public static void main(String[] args) {
        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Integer tem = i;
            new Thread(()->{
                integers.add(tem);
                System.out.println(integers);
            }).start();
        }
    }
}

多跑几次就会出错

Exception in thread "Thread-1" Exception in thread "Thread-0" java.util.ConcurrentModificationException

解决方案

1.用Vector

2.用 Collections.synchronizedList(new ArrayList<>());

3.用CopyOnWriteArrayList 底层的add get方法用到了可重入锁ReentrantLock。而且用的不是同一个锁,实现读写分离,写时复制

同样set也不安全

解决方案

1.Collections.synchronizedSet(new HashSet<>());

2.Set<Integer> set = new CopyOnWriteArraySet<>(); 底层CopyOnWriteArrayList

set底层是HashMap set的add方法是将调用HashMap的put方法,key值放入元素,value值放入的是一个空Obejct private static final Object PRESENT = new Object();

package com.collection;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @author liuxu
 * @date 2021/11/11 22:28
 */
public class ArrayListDemo {
    public static void main(String[] args) {
        List<Integer> integers = new Vector<>();
        //List<Integer> integers = Collections.synchronizedList(new ArrayList<>());
        // List<Integer> integers = new CopyOnWriteArrayList();
        // Set<Integer> set = Collections.synchronizedSet(new HashSet<>());
        for (int i = 0; i < 10; i++) {
            Integer tem = i;
            new Thread(()->{
                integers.add(tem);
                System.out.println(integers);
            }).start();
        }
    }
}

map 同样不安全

解决方案

1.用Map<String,String> map = new ConcurrentHashMap<>();

2.用Map<String,String> map = Collections.synchronizedMap(new HashMap<>());

package com.collection;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuxu
 * @date 2021/11/11 22:58
 */
public class MapDemo {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        for (int i = 0; i < 20; i++) {
            int tem = i;
            new Thread(()->{
                map.put("key"+tem,"val"+tem);
                System.out.println(map);
            }).start();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值