Java 8 introduces a parallel algorithm for multi-threaded sorting of arrays, in the form of the overloaded Arrays.sort() methods.
Why does it not also provide a Collections.parallelSort(), for multi-threaded sorting of a List?
parallelSort() method uses concept of MultiThreading which makes the sorting faster as compared to normal sorting method. Note : Different time intervals will be printed But parallel sort will be done before normal sort.
Parallel Sort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool. Fork/Join implements a work stealing algorithm where in a idle thread can steal tasks queued up in another thread.
Java Parallel Array Sorting. Java provides a new additional feature in Array class which is used to sort array elements parallel. New methods has added to java. util. Arrays package that use the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.
Collections class provides static methods for sorting the elements of a collection. If collection elements are of a Set type, we can use TreeSet. However, we cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.
A List does not necessarily allow efficient implementation of the same parallel sorting algorithms that an array does. You may be able to directly apply it to an ArrayList, but most likely not to a LinkedList, due to its lack of efficient random access. There are efficient multi-threaded sorting algorithms for that kind of list, but they are different from a random-access list.
And, in fact, the thread-safe implementation of the List interface may not support efficient external multi-threaded sorting at all, due to synchronization. Providing a generic sorting algorithm for those would be impossible, and in fact a parallel algorithm might be slower on them than a sequential algorithm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With