Not violating "compareTo" contract in a changing list
This is kind of a general java question and doesn't relate too much to Android.
In my application I have a list of some objects of a medium complex class which implements Comparable
. The compareTo()
method's result depends on some of the fields. The method is consistent and doesn't violate the comparison contract, when the elements aren't modified during the comparison.
Now to the problem. The list must be sorted once in a while. But simultaneously, some items may be updated by a background task, which means that the result of compareTo()
changes. That's where the contrac开发者_StackOverflow社区t may be violated.
I understand, that the easiest answer would be, not to update the list during the sorting at all. But it's a mobile application. It has to be responsive and the user may refresh the list whenever he wants.
What would be an elegant approach to this problem? Using synchronizing or some kind of lock?
Thanks in advance.
I would synchronize over the list both times and it would be fine.
精彩评论