Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays sort method behavior

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.

like image 265
enrique7mc Avatar asked May 21 '14 04:05

enrique7mc


People also ask

What is Array sort () method in JavaScript?

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.

How does the sort () method work?

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.

How do you sort an array in Python?

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.

How to sort an array in ascending order in Java?

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.


1 Answers

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());
like image 112
Elliott Frisch Avatar answered Oct 10 '22 12:10

Elliott Frisch