Reputation: 15549
How the following can generate the error given in the title:
Integer[] length = whatever();
Arrays.sort(length);
All the questions I found about this exception have something to do with the comparison method. However, here the sorted objects are standard Integer
instances.
I think this might have something to do with multi-threading, because the array returned by whatever()
is something that can be recomputed by other threads.
Upvotes: 1
Views: 71
Reputation: 311796
java.lang.Integer
's compareTo(Integer)
method does not violate any contract about sorting, of course. As you noted, though, Arrays.sort
(and probably any other sorting algorithm implemented in the JDK) implicitly assumes that it's the only one manipulating the array. If this array is accessible from multiple threads, you'll have to protect the access to it, e.g., but using a synchronized
block.
Upvotes: 1