[concurrency-interest] Re: AtomicInteger and AtomicLong should implement Number

Larry Riedel larryr@saturn.sdsu.edu
9 Jan 2004 18:45:34 -0000


> Sorting or comparing mutable values is extremely error prone.

It had not occurred to me that only immutable objects should be
created by classes which implement Comparable.  This sounds like
something that should definitely be in the API documentation for
Comparable; and I think it makes sense to also have ByteBuffer
(etc) updated to throw an exception if compareTo() is invoked
for a ByteBuffer which is not ReadOnly.


> > Of course the same arguments can be made for AtomicIntegerS 
> > being Comparable.  Why aren't AtomicIntegerS comparable? 
> 
> Because you cannot rely on the result of the comparison
> anyway, since the values may be immediately changed by other
> threads after you compare but before you use the comparison
> result. To avoid this, you would have to use locking, and
> those classes exist precisely to avoid locking.

It is not my impression it is generally accepted that unless
there is a problem, the relative ordering of any two objects
now and later must be the same.  Currently Java in particular
seems to acknowledge that during the normal course of events,
the relative ordering of two objects may change.

I would expect developers to use their objects in a sane way,
and I think the language/API should primarily try to facilitate
that, not try to preclude them from doing things which might
be a mistake in some contexts.


Larry