Re: Generics
Andreas Leitgeb wrote:
Lew <lew@lewscanon.com> wrote:
Why do people insist on calling Vector "thread safe"? Having synchronized
methods doesn't make it thread safe.
I fail to see in which way a Vector was any *less* "thread safe" than
a Collections.synchronizedList(new ArrayList(...)).
It isn't.
Docu for synchronizedList says:
" Returns a synchronized (thread-safe) ..."
Note, it doesn't say "synchronized and also thread-safe",
but rather seems to consider those two to be like synonyms.
Yes, and that is one instance of Sun's documentation leading people astray.
Individual methods are synchronized; using two synchronized methods separately
is not inherently thread-safe; consider check-and-set operations.
The OP's problem seemed to call for a check-and-set (check capacity, and
increase if needed, then add()). Even with Vector or synchronizedList(), this
opens the barn door for a race condition without explicit synchronization. As
I said, in this case I wouldn't use Vector or synchronizedList(); I'd declare
the variable a plain ArrayList for the extra methods the OP wanted (or just
List, since I think they're not really needed), and dispense with implicit
synchronization altogether.
Docu for Vector only says:
" Vector is synchronized.
What operations can I do on a synchronizedList without extra
synchronization-block, that I cannot do on a Vector likewise
and with same level of real thread-safety?
None.
ArrayList and synchronizedList() give you Lists without the non-collections
methods of Vector and without Enumeration. I like synchronizedList() better
than Vector because it lacks unneeded machinery; it Occam's Razors itself into
my heart.
It's not what List has that Vector doesn't; it's what Vector has that List
doesn't that makes me excoriate Vector.
--
Lew