I'm reading the book "Java SE 8 for the really impatient", in the first chapter I came across with the next exercise question:
Is the comparator code in the Arrays.sort method called in the same thread as the call to sort or a different thread?
I've searched the javadoc for the Arrays.sort overloading which takes a Comparator argument but it doesn't specify anything about threads. I assume that for performance reasons that code could be executed in another thread, but it is just a guess.
Below is the example of Array sort () method. The arr.sort () method is used to sort the array in place in a given order according to the compare () function. If the method is omitted then the array is sorted in ascending order.
The sort () method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.
The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
Using the sort () Method In Java, Arrays is the class defined in the java.util package that provides sort () method to sort an array in ascending order. It uses Dual-Pivot Quicksort algorithm for sorting.
You can always test it by logging the id
of Thread.currentThread()
.
Add something this just before calling sort()
and in your compare()
method.
logger.debug("Thread # " + Thread.currentThread().getId());
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