This content has been marked as final. Show 1 reply
Vrijmetse wrote:You have not added any synchronization around your Vector operations. Vector's individual methods are synchronized, but that only extends to the execution of those methods. There's no way for that syncing to know that you're doing stuff in a for loop. If you want to complete the for loop without anything else affecting that Vector, then you need a sync block around the body of the loop, and anything else that acceses the Vector must also sync on the same object. The Vector itself is a good candidate for the lock here.
1. Why the 2nd thread is able to access the someVec ( Vector) when the 1st thread is still adding some values?
(There may be other problems as well. I haven't read your code closely. Just noticed the for loop and lack of syncing.)
2. When I changed someVec to someArrayList the result is still the same. I wonder why ?Same reason.
3. Lastly, Can anyone modify my code so only either a Vector or a Arraylist will make the program run without error ?No.