Arrays.sort(带比较器)- 相同或不同的线程?

Arrays.sort (with Comparator) - same or different thread?

Arrays.sort方法中的比较器代码是否在与 调用排序或不同的线程?

我是在 JDK 8.

的背景下问这个问题的

我认为答案是它在同一个线程中被调用,但我不是 100% 确定。如果回答这个问题的人提供一些参考或其他类型的详细解释(除了简单的是或否),我会很高兴。

答案是否定的。排序(在 Arrays.sort) is implemented with DualPivotQuicksort 中,来自文档:

This class implements the Dual-Pivot Quicksort algorithm by Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations. All exposed methods are package-private, designed to be invoked from public methods (in class Arrays) after performing any necessary array bounds checks and expanding parameters into the required forms.

正如您在 the implementation 中看到的那样 - 它不会启动任何线程。

此外,还有 parallelSort 方法使用 ForkJoin 公共池来执行并行执行。这是非常明确的,正如其他一些评论者已经提到的那样 - JDK API 对此类问题含糊不清的可能性很小。