concurrent set的创建

从Java源代码的角度来说,HashSet是基于HashMap来实现的。但是并没有存在ConcurrentHashMap对应的ConcurrentHashSet。反而,有一个类似CopyOnWriteArrayList的CopyOnWriteArraySet。接下来,我们一起看看如何创建线程安全的set。

1、CopyOnWriteArraySet:

和CopyOnWriteArrayList实现原理一样,适合读多写少的操作。详情见:https://blog.csdn.net/liuxiao723846/article/details/88172222

2、Collections.synchronizedSet(set):

使用java.utils.Collections的类,这种方式生成的Set都是全局锁,性能比较差。

Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
Set<String> synchronizedSet = Collections.synchronizedSet(set);
synchronizedSet.add("e");
System.out.println("Collections.synchronizedSet:"+synchronizedSet);

输出:
Collections.synchronizedSet:[a, b, c, e]

3、Collections.newSetFromMap(map):

使用java.utils.Collections的类,其实质还是通过ConcurrentHashMap实现线程安全的。

Map<String,Boolean> map = new HashMap<>();
Set<String> newSetFromMap = Collections.newSetFromMap(map);
newSetFromMap.add("d");
System.out.println("Collections.newSetFromMap:"+newSetFromMap);

输出:Collections.newSetFromMap:[d]

注:这种方式构造的Set,需要传入一个空的map,否则会报如下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Map is non-empty
    at java.util.Collections$SetFromMap.<init>(Collections.java:5451)
    at java.util.Collections.newSetFromMap(Collections.java:5437)

4、ConcurrentHashMap.newKeySet():

这是jdk8提供的一个方法,本质也是通过concurrentHashMap实现的。

KeySetView<String, Boolean> newKeySet = ConcurrentHashMap.newKeySet();
newKeySet.add("a");
newKeySet.add("a2");
newKeySet.add("a3");
newKeySet.add("a4");

System.out.println("ConcurrentHashMap.newKeySet():"+newKeySet);

输出:ConcurrentHashMap.newKeySet():[a, a2, a3, a4]

5、Guava15的Sets.newConcurrentHashSet():

guava提供的一个方法,本质是使用了Collections.newSetFromMap。

Set<String> newConcurrentHashSet = Sets.newConcurrentHashSet();
newConcurrentHashSet.add("1");
newConcurrentHashSet.add("2");
newConcurrentHashSet.add("13");
System.out.println("Sets.newConcurrentHashSet:"+newConcurrentHashSet);

输出:Sets.newConcurrentHashSet:[1, 2, 13]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值