java Vector and thread safety
java, multithreading, thread-safety, vector
Solution
I assume you are referring to `java.util.Vector`.
Actually `Vector.size()` is synchronized and will return a value consistent with the vector's state (when the thread calling `size()` enters the monitor.) If it returns 42, then at some point in time the vector contained exactly 42 elements.
If you're adding items in a loop in another thread then you cannot predict the exact size, but it should be fine for monitoring purposes.
Problem
I'm wondering if this code will do any trouble: I have a vector that is shared among many threads. Every time a thread has to add/remove stuff from the vector I do it under a `synchronized` block. However, the main thread has a call: ``` System.out.println("the vector's size: "+ vec.size()); ``` which isn't `synchronized`. Should this cause trouble?