Re: Vector (was Re: Change character in string)
Andreas Leitgeb wrote:
Bent C Dalager <bcd@pvv.ntnu.no> wrote:
I am going to take this as an invitation to vent my pet peeve with
Vector again (even if I don't really understand what the contest is
all about). My apologies if it has been mentioned before in this
thread - I'm something of a latecomer to the discussion.
[demonstration of deadlock-weakness of Vector w.r.t. .equals()]
No, it hasn't and I'm very grateful to this contribution.
Current score:
minus 100 points against Vector.
I lost.
Here is the source of the underlying 'SynchronizedList' nested class's
'equals()' method:
public boolean equals(Object o) {
synchronized(mutex) {return list.equals(o);}
}
That's good, because if 'equals()' weren't synchronized it would be broken.
I am grateful to Bent also, because I never knew about this flaw with
'Vector'. It is good to have more than just an aesthetic argument for my choice.
Now I have quite the mental exercise ahead of me to figure out why
'Vector#equals()' deadlocks and 'SynchronizedList#equals()' doesn't.
Here's the source from 'Vector':
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
...
public synchronized boolean equals(Object o) {
return super.equals(o);
}
...
}
How interesting that Sun, who usually espouses
"opening-brace-on-the-same-line", gives it a separate line at the class level
here.
--
Lew