有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (6) 个答案

  1. # 1 楼答案

    您可以参考the javadoc,这解释了如果数组足够大,算法将使用多个线程:

    The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. [...] The ForkJoin common pool is used to execute any parallel tasks.

  2. # 2 楼答案

    两种算法之间的主要区别如下:

    1。数组。sort():是一种顺序排序

    • API使用单线程进行操作
    • API执行该操作所需的时间稍长一些

    2。数组。ParallelSort():是一种并行排序

    API使用多个线程

    • 与Sort()相比,API花费的时间更少

    为了获得更多的结果,我想我们都必须等待Java8!!干杯

  3. # 3 楼答案

    简而言之,parallelSort使用多个线程。如果你真的想知道,这个article有更多的细节

  4. # 4 楼答案

    数组。并行排序():

    The method uses a threshold value and any array of size lesser than the threshold value is sorted using the Arrays#sort() API (i.e sequential sorting). And the threshold is calculated considering the parallelism of the machine, size of the array and is calculated as:

    private static final int getSplitThreshold(int n) {
     int p = ForkJoinPool.getCommonPoolParallelism();
     int t = (p > 1) ? (1 + n / (p << 3)) : n;
     return t < MIN_ARRAY_SORT_GRAN ? MIN_ARRAY_SORT_GRAN : t;
    }
    

    Once its decided whether to sort the array in parallel or in serial, its now to decide how to divide the array in to multiple parts and then assign each part to a Fork/Join task which will take care of sorting it and then another Fork/Join task which will take care of merging the sorted arrays. The implementation in JDK 8 uses this approach:

    • Divide the array into 4 parts.

    • Sort the first two parts and then merge them.

    • Sort the next two parts and then merge them. And the above steps are repeated recursively with each part until the size of the part to sort is not lesser than the threshold value calculated above.

    您还可以在Javadoc中阅读实现细节

    The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort method. The algorithm requires a working space no greater than the size of the specified range of the original array. The ForkJoin common pool is used to execute any parallel tasks.

    数组。排序():

    This uses merge sort OR Tim Sort underneath to sort the contents. This is all done sequentially, even though merge sort uses divide and conquer technique, its all done sequentially.

    Source

  5. # 5 楼答案

    并行排序使用线程-每个线程获得列表的一个块,所有块都并行排序。然后将这些已排序的块合并为一个结果

    当集合中有大量元素时,速度会更快。在较大的集合上,并行化(拆分成块并合并)的开销相当小,但在较小的集合上,并行化的开销很大

    看看这个表(当然,结果取决于CPU、内核数量、后台进程等):

    enter image description here

    取自此链接:http://www.javacodegeeks.com/2013/04/arrays-sort-versus-arrays-parallelsort.html

  6. # 6 楼答案

    从这个link

    Current sorting implementations provided by the Java Collections Framework (Collections.sort and Arrays.sort) all perform the sorting operation sequentially in the calling thread. This enhancement will offer the same set of sorting operations currently provided by the Arrays class, but with a parallel implementation that utilizes the Fork/Join framework. These new API’s are still synchronous with regard to the calling thread as it will not proceed past the sorting operation until the parallel sort is complete.