JCTools, 场景特化的并发工具

时间:2023-03-09 07:22:22
JCTools, 场景特化的并发工具

同上一篇一样,在jmap -histo中发现MpscChunkedArrayQueue类的实例比较多,javadoc看了下,其原来是出自JC Tools,https://github.com/JCTools/JCTools。

通过官网,我们可以看到,它针对实际的场景对JDK自带的并发队列做了一些特定的优化,如下:

Java Concurrency Tools for the JVM. This project aims to offer some concurrent data structures currently missing from the JDK:

  • SPSC/MPSC/SPMC/MPMC variations for concurrent queues:

    • SPSC - Single Producer Single Consumer (Wait Free, bounded and unbounded)
    • MPSC - Multi Producer Single Consumer (Lock less, bounded and unbounded)
    • SPMC - Single Producer Multi Consumer (Lock less, bounded)
    • MPMC - Multi Producer Multi Consumer (Lock less, bounded)
  • SPSC/MPSC linked array queues offer a balance between performance, allocation and footprint

  • An expanded queue interface (MessagePassingQueue):

    • relaxedOffer/Peek/Poll: trade off conflated guarantee on full/empty queue state with improved performance.
    • drain/fill: batch read and write methods for increased throughput and reduced contention

不过整体浏览了下,文档较少,benchmark提供了用例但没有看到结果,真用到的时候,可以看下到底性能可以提升多少。有一文章对源码进行了分析(http://blog.****.net/youaremoon/article/details/50351929),不过说实话,不知道现在为什么很多人喜欢share源码分析,仅仅只是源码分析,对场景化的适用条件以及原理和实现机制倒是考虑的很少。举个例子,https://www.qcloud.com/community/article/904925001482373849?utm_source=Community&utm_medium=article904925001482373849&utm_campaign=kyzg就是一篇典型的文章,是为了说明看过源码了、还是理解了。通常,个人认为理解如何实现、适用场景及优缺点是同等重要的,否则就只顾赶路、却不关心自己在哪里了。