Originally posted by Alex Kravets:
But if I switch to unsynchronized collection, won't my code become thread unsafe?
Only if you have multiple threads accessing the
same Vector object. Note that each call to your method returns a different Vector object: "data = new Vector(50,5);".
The only way this could happen is if after the call to retrieve data -- "Vector data = DBDriver.doSelect(...)" above -- you handed the "data" Vector to multiple Threads for processing. If instead each Thread processes its own Vector, they cannot interfere with each other.
You might want to go through Sun's multi-threading tutorial to make sure you understand exactly what thread-safe and synchronization means for objects versus classes.
Think of it this way. If you have a car and I have a different car, we can both drive our cars at the same time. The "idea of a car" is not synchronized. If, however, we
share a single car, then only one of us can drive it at the same time. Thus, "driving a car" is a synchronized operation on each car individually.