Arrays.sort versus Arrays.parallelSort | Java, JVM and beyond
public static void sort(Object[] a) { if (LegacyMergeSort.userRequested) legacyMergeSort(a); else ComparableTimSort.sort(a); } This is all done sequentially, even though merge sort uses divide and conquer technique, its all done sequentially. Come Java 8, there is a new API introduced for sorting which is Arrays#parallelSort . This is does the sorting in parallel. Interesting right! Lets see how it does… Arrays#parallelSort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool. This is called eating your own dog food . Fork/Join implements a work stealing algorithm where in a idle thread can steal tasks queued up in another thread. An overview of Arrays#parallelSort: 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,Read full article from Arrays.sort versus Arrays.parallelSort | Java, JVM and beyond
No comments:
Post a Comment