当前位置: 移动技术网 > IT编程>开发语言>Java > JCTools 简介

JCTools 简介

2020年03月20日  | 移动技术网IT编程  | 我要评论

倚天屠龙之风神传奇,可爱颂中文翻译,魔宇文库

jctools

早在96年就有论文提出了无锁队列的概念,再到后来 disruptor,高性能已得到生产的验证。此处介绍的 jctools 中的高性能队列,其性能丝毫不输于 disruptor。

jctools (java concurrency tools) 提供了一系列非阻塞并发数据结构(标准 java 中缺失的),当存在线程争抢的时候,非阻塞并发数据结构比阻塞并发数据结构能提供更好的性能。

jctools 是一个开源工具包,在 apache license 2.0 下发布,并在 netty、rxjava 等诸多框架中被广泛使用。

jctools 的开源 github 仓库:https://github.com/jctools/jctools

在 maven 中引入 jctools jar 包就能使用 jctools 了:

        <dependency>
            <groupid>org.jctools</groupid>
            <artifactid>jctools-core</artifactid>
            <version>3.0.0</version>
        </dependency>

jctools 中主要提供了 map 以及 queue 的非阻塞并发数据结构:

非阻塞 map

  • concurrentautotable(后面几个map/set结构的基础)
  • nonblockinghashmap
  • nonblockinghashmaplong
  • nonblockinghashset
  • nonblockingidentityhashmap
  • nonblockingsetint

nonblockinghashmap 是对 concurrenthashmap 的增强,对多 cpu 的支持以及高并发更新提供更好的性能。

nonblockinghashmaplong 是 key 为 long 型的 nonblockinghashmap。

nonblockinghashset 是对 nonblockinghashmap 的简单包装以支持 set 的接口。

nonblockingidentityhashmap 是从 nonblockinghashmap 改造来的,使用 system.identityhashcode() 来计算哈希。

nonblockingsetint 是一个使用 cas 的简单的 bit-vector。

非阻塞 queue

jctools 提供的非阻塞队列分为 4 类,可以根据不同的应用场景选择使用:

  • spsc-单一生产者单一消费者(有界和无界)
  • mpsc-多生产者单一消费者(有界和无界)
  • spmc-单生产者多消费者(有界)
  • mpmc-多生产者多消费者(有界)

“生产者”和“消费者”是指“生产线程”和“消费线程”。

        // spsc-有界/无界队列
        queue<string> spscarrayqueue = new spscarrayqueue(16);
        queue<string> spscunboundedarrayqueue = new spscunboundedarrayqueue(2);
        // spmc-有界队列
        queue<string> spmcarrayqueue = new spmcarrayqueue<>(16);
        // mpsc-有界/无界队列
        queue<string> mpscarrayqueue = new mpscarrayqueue<>(16);
        queue<string> mpscchunkedarrayqueue = new mpscchunkedarrayqueue<>(1024, 8 * 1024);
        queue<string> mpscunboundedarrayqueue = new mpscunboundedarrayqueue<>(2);
        // mpmc-有界队列
        queue<string> mpmcarrayqueue = new mpmcarrayqueue<>(16);



参考资料:


https://blog.csdn.net/theludlows/article/details/90646236

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网