说明:阅读本文之前,请先掌握本文前置知识: 跳表 核心原理 图解,以及ConcurrentSkipListMap - 秒懂

JUC 高并发工具类(3文章)与高并发容器类(N文章) :


1 ConcurrentSkipListSet简介

ConcurrentSkipListSet,是J.U.C新增的一个集合工具类,顾名思义,它是一种SET类型。

SET类型,在数学上称为“集合”,具有互异性、无序性的特点,也就是说SET中的任意两个元素均不相同(即不包含重复元素),且元素是无序的。

JDK提供的默认SET实现——HashSet,其实就是采用“组合”的方式——内部引用了一个HashMap对象,以此实现SET的功能。

ConcurrentSkipListSet与ConcurrentSkipListMap的关系,与SET与HashMap的关系类似,就是采用“组合”的方式: ConcurrentSkipListSet组合了一个ConcurrentSkipListMap,将元素作为 ConcurrentSkipListMap的key存放。

2 ConcurrentSkipListSet原理

2.1 ConcurrentSkipListSet的类继承图

我们来看下ConcurrentSkipListSet的类继承图:

2.2 内部结构

ConcurrentSkipListSet的数据结构,如下图所示:

说明:

(01) ConcurrentSkipListSet继承于AbstractSet。因此,它本质上是一个集合。

(02) ConcurrentSkipListSet实现了NavigableSet接口。因此,ConcurrentSkipListSet是一个有序的集合。

(03) ConcurrentSkipListSet是通过组合ConcurrentSkipListMap实现的。它包含一个ConcurrentNavigableMap对象m,而m对象实际上是ConcurrentNavigableMap的实现类ConcurrentSkipListMap的实例。ConcurrentSkipListMap中的元素是key-value键值对;而ConcurrentSkipListSet是集合,它只用到了ConcurrentSkipListMap中的key!

2.3 构造器

ConcurrentSkipListSet的实现非常简单,其内部引用了一个ConcurrentSkipListMap对象,所有API方法均委托ConcurrentSkipListMap对象完成:

public class ConcurrentSkipListSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable { /**
* The underlying map. Uses Boolean.TRUE as value for each
* element. This field is declared final for the sake of thread
* safety, which entails some ugliness in clone().
*/
private final ConcurrentNavigableMap<E, Object> m; public ConcurrentSkipListSet() {
m = new ConcurrentSkipListMap<E, Object>();
} public ConcurrentSkipListSet(Comparator<? super E> comparator) {
m = new ConcurrentSkipListMap<E, Object>(comparator);
} public ConcurrentSkipListSet(Collection<? extends E> c) {
m = new ConcurrentSkipListMap<E, Object>();
addAll(c);
} public ConcurrentSkipListSet(SortedSet<E> s) {
m = new ConcurrentSkipListMap<E, Object>(s.comparator());
addAll(s);
} ConcurrentSkipListSet(ConcurrentNavigableMap<E, Object> m) {
this.m = m;
} // ...
}

从上述代码可以看出,ConcurrentSkipListSet在构造时创建了一个ConcurrentSkipListMap对象,并由字段m引用,所以其实ConcurrentSkipListSet就是一种跳表类型的数据结构,其平均增删改查的时间复杂度均为O(logn)。

2.4 核心方法

我们来看下ConcurrentSkipListSet是如何实现API方法的:


public int size() {
return m.size();
} public boolean isEmpty() {
return m.isEmpty();
} public boolean contains(Object o) {
return m.containsKey(o);
} public boolean add(E e) {
return m.putIfAbsent(e, Boolean.TRUE) == null;
} public boolean remove(Object o) {
return m.remove(o, Boolean.TRUE);
} public void clear() {
m.clear();
} //...

从上述代码可以看出,所有操作均是委托ConcurrentSkipListMap对象完成的。重点看下add方法:

public boolean add(E e) {

​ return m.putIfAbsent(e, Boolean.TRUE) == null;

}

ConcurrentSkipListMap对键值对的要求是均不能为null,所以ConcurrentSkipListSet在插入元素的时候,用一个Boolean.TRUE对象(相当于一个值为true的Boolean型对象)作为value,同时putIfAbsent可以保证不会存在相同的Key。

所以,最终跳表中的所有Node结点的Key均不会相同,且值都是Boolean.True

3 ConcurrentSkipListSet适用场景

ConcurrentSkipListSet是线程安全的有序的集合,适用于高并发的场景。

ConcurrentSkipListSet和TreeSet

ConcurrentSkipListSet和TreeSet,它们虽然都是有序的集合。但是,第一,它们的线程安全机制不同,TreeSet是非线程安全的,而ConcurrentSkipListSet是线程安全的。第二,ConcurrentSkipListSet是通过ConcurrentSkipListMap实现的,而TreeSet是通过TreeMap实现的。

4 使用例子

import java.util.*;
import java.util.concurrent.*; /*
* ConcurrentSkipListSet是“线程安全”的集合,而TreeSet是非线程安全的。
*
* 下面是“多个线程同时操作并且遍历集合set”的示例
* (01) 当set是ConcurrentSkipListSet对象时,程序能正常运行。
* (02) 当set是TreeSet对象时,程序会产生ConcurrentModificationException异常。
*
* @author skywang
*/
public class ConcurrentSkipListSetDemo1 { // TODO: set是TreeSet对象时,程序会出错。
//private static Set<String> set = new TreeSet<String>();
private static Set<String> set = new ConcurrentSkipListSet<String>();
public static void main(String[] args) { // 同时启动两个线程对set进行操作!
new MyThread("a").start();
new MyThread("b").start();
} private static void printAll() {
String value = null;
Iterator iter = set.iterator();
while(iter.hasNext()) {
value = (String)iter.next();
System.out.print(value+", ");
}
System.out.println();
} private static class MyThread extends Thread {
MyThread(String name) {
super(name);
}
@Override
public void run() {
int i = 0;
while (i++ < 10) {
// “线程名” + "序号"
String val = Thread.currentThread().getName() + (i%6);
set.add(val);
// 通过“Iterator”遍历set。
printAll();
}
}
}
}

运行程序,结果如下:

a1, b1,
a1, a1, a2, b1,
b1, a1, a2, a3, b1, a1, a2, a3, a1, a4, b1, b2,
a2, a1, a2, a3, a4, a5, b1, b2,
a3, a0, a4, a5, a1, b1, a2, b2,
a3, a0, a4, a1, a5, a2, b1, a3, b2, a4, b3,
a5, a0, b1, a1, b2, a2, b3,
a3, a0, a4, a1, a5, a2, b1, a3, b2, a4, b3, a5, b4,
b1, a0, b2, a1, b3, a2, b4,
a3, a0, a4, a1, a5, a2, b1, a3, b2, a4, b3, a5, b4, b1, b5,
b2, a0, a1, a2, a3, a4, a5, b3, b1, b4, b2, b5,
b3, a0, b4, a1, b5,
a2, a0, a3, a1, a4, a2, a5, a3, b0, a4, b1, a5, b2, b0, b3, b1, b4, b2, b5, b3,
b4, a0, b5,
a1, a2, a3, a4, a5, b0, b1, b2, b3, b4, b5,
a0, a1, a2, a3, a4, a5, b0, b1, b2, b3, b4, b5,
a0, a1, a2, a3, a4, a5, b0, b1, b2, b3, b4, b5,
a0, a1, a2, a3, a4, a5, b0, b1, b2, b3, b4, b5,

回到疯狂创客圈

疯狂创客圈 - Java高并发研习社群,为大家开启大厂之门

ConcurrentSkipListSet - 秒懂的更多相关文章

  1. CyclicBarrier 原理(秒懂)

    疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 面试必备 + 面试必备 [博客园总入口 ] 疯狂创客圈 经典图书 : <Sprin ...

  2. ConcurrentSkipListMap - 秒懂

    疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 面试必备 + 面试必备 [博客园总入口 ] 疯狂创客圈 经典图书 : <Sprin ...

  3. JUC并发包与容器类 - 面试题(一网打净,持续更新)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  4. 【JUC】JDK1.8源码分析之ConcurrentSkipListSet(八)

    一.前言 分析完了CopyOnWriteArraySet后,继续分析Set集合在JUC框架下的另一个集合,ConcurrentSkipListSet,ConcurrentSkipListSet一个基于 ...

  5. 30秒懂SQL中的join(2幅图+30秒)

    废话不多说,直接上图秒懂. t1表的结构与数据如下: t2表的结构与数据如下: inner join select * from t1 inner join t2 on t1.id = t2.id; ...

  6. Java多线程系列--“JUC集合”06之 ConcurrentSkipListSet

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...

  7. 并发容器之ConcurrentSkipListSet

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...

  8. 死磕 java集合之ConcurrentSkipListSet源码分析——Set大汇总

    问题 (1)ConcurrentSkipListSet的底层是ConcurrentSkipListMap吗? (2)ConcurrentSkipListSet是线程安全的吗? (3)Concurren ...

  9. 并发容器学习—ConcurrentSkipListMap与ConcurrentSkipListSet 原

    一.ConcurrentSkipListMap并发容器 1.ConcurrentSkipListMap的底层数据结构     要学习ConcurrentSkipListMap,首先要知道什么是跳表或跳 ...

随机推荐

  1. VS Code插件推荐-Settings Sync

    Settings Sync功能 将vscode的本地设置.插件保存至远端,方便保存 Usage 插件市场安装Setting Sync之后,⌘+P输入>sync,即可看到相关操作,选中点击之后官方 ...

  2. php实现微信推送消息

    一.<?phpnamespace Org\Weixin;class OrderPush{ protected $appid; protected $secrect; protected $acc ...

  3. Spring Cloud Alibaba(8)---Feign服务调用

    Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...

  4. Python | Pandas数据清洗与画图

    准备数据 2016年北京PM2.5数据集 数据源说明:美国驻华使馆的空气质量检测数据 数据清洗 1. 导入包 import numpy as np import matplotlib.pyplot a ...

  5. 风变编程(Python自学笔记)第10关-工作量计算器

    1.%f的意思是格式化字符串为浮点型,%.1f的意思是格式化字符串为浮点型,并保留1位小数. 2.向上取整:ceil() 使用ceil()方法时需要导入math模块,例如 1 >>> ...

  6. 2Spring对象创建小结

    Spring的对象创建 Spring学习笔记 周芋杉2021/5/15 原理:工厂设计模式,通过反射创建对象. Spring工厂分类 非web环境:ClassPathXmlApplicationCon ...

  7. 登陆框select绕过

    0x00 原理   思路来自美团杯2021,本来说出题人已经把select通过正则过滤了,就不该总是往用select进行查询那方面想-> select id from users where u ...

  8. [DB] 数据库概述

    基本概念 关系模型:包括关系数据结构.关系操作集合.关系完整性约束三部分 关系型数据库:建立在关系模型基础上的数据库.由多张能互相联接的二维行列表格组成. 非关系型数据库(Nosql(Not Only ...

  9. [bug] CDH 安装 Error : No matching Packages to list

    信息 分析 我的系统是CentOS 7,而 cm 安装包是配合 redhat 6 的,应该选择 redhat 7 目录下的包 参考 https://community.cloudera.com/t5/ ...

  10. 详解Linux中的cat文本输出命令用法

    作系统 > LINUX >   详解Linux中的cat文本输出命令用法 Linux命令手册   发布时间:2016-01-14 14:14:35   作者:张映    我要评论   这篇 ...